You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This PR fixes a bug which made it possible to put variants as null or undefined into the isExperienceEntry as valid object. As the variants didn't get defaulted to an empty array, the ExperienceMapper.mapExperience function failed.
PR Type
Bug fix, Tests
Description
Added tests to ensure null and invalid variants are not accepted in ExperienceEntry.
Added tests to default missing or null variants to an empty array in Experiment and Experience.
Added test to ensure the type of variants is preserved in ExperienceMapper.
Fixed the parse and safeParse functions in Experience and Experiment to correctly handle variants.
Changes walkthrough 📝
Relevant files
Tests
ExperienceEntry.spec.ts
Add tests for invalid and null variants in ExperienceEntry
Possible Bug: The use of TypeScript's @ts-expect-error comments in tests could mask other potential type errors that are not intended to be tested. Consider using more precise type assertions or handling.
Code Duplication: Similar test scenarios and expectations are repeated across multiple test files (ExperienceEntry.spec.ts, Experiment.spec.ts, and Experience.spec.ts). Consider abstracting common logic or expectations to reduce redundancy and improve maintainability.
Use a type-safe approach to ensure output.variants is of type T[]
Instead of casting output.variants to T[], consider using a type-safe approach to ensure output.variants is of type T[]. This can be done by validating the type during the parsing process.
-variants: output.variants as T[],+variants: Array.isArray(output.variants) ? output.variants as T[] : [],
Apply this suggestion
Suggestion importance[1-10]: 8
Why: The suggestion to use a type-safe approach instead of a simple cast is crucial for preventing runtime errors and ensuring data integrity. This is a significant improvement in the code's robustness.
8
Use a type-safe approach to ensure output.data.variants is of type T[]
Instead of casting output.data.variants to T[], consider using a type-safe approach to ensure output.data.variants is of type T[]. This can be done by validating the type during the parsing process.
-variants: output.data.variants as T[],+variants: Array.isArray(output.data.variants) ? output.data.variants as T[] : [],
Apply this suggestion
Suggestion importance[1-10]: 8
Why: Similar to the previous suggestion, ensuring type safety for output.data.variants is essential for maintaining the integrity and reliability of the data structure, making this a valuable improvement.
8
Best practice
Update type definitions to handle null values instead of suppressing TypeScript errors
Instead of using @ts-expect-error to suppress TypeScript errors, consider updating the type definitions to handle null values for nt_variants. This will make the code more robust and maintainable.
-// eslint-disable-next-line @typescript-eslint/ban-ts-comment-// @ts-expect-error-nt_variants: null,+nt_variants: null as unknown as SomeType[],
Apply this suggestion
Suggestion importance[1-10]: 7
Why: The suggestion correctly identifies a potential improvement in handling null values for nt_variants by updating type definitions instead of suppressing errors. This approach enhances maintainability and robustness.
7
Add a type guard function to check for the hidden property in the variant object
Add a type guard function to check if the variant object has the hidden property, which will make the code more readable and type-safe.
-if (!('hidden' in variant)) {- // Yeay! It correctly inferred the type of the property "foo" on the variant+const hasHiddenProperty = (variant: any): variant is { hidden: boolean } => 'hidden' in variant;+if (!hasHiddenProperty(variant)) {
expect(variant.foo).toBe('bar');
}
Apply this suggestion
Suggestion importance[1-10]: 6
Why: Adding a type guard function as suggested would improve readability and type safety. However, the improvement is relatively minor and specific to this instance.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
This PR fixes a bug which made it possible to put
variants
asnull
orundefined
into theisExperienceEntry
as valid object. As thevariants
didn't get defaulted to an empty array, theExperienceMapper.mapExperience
function failed.PR Type
Bug fix, Tests
Description
null
and invalid variants are not accepted inExperienceEntry
.Experiment
andExperience
.ExperienceMapper
.parse
andsafeParse
functions inExperience
andExperiment
to correctly handle variants.Changes walkthrough 📝
ExperienceEntry.spec.ts
Add tests for invalid and null variants in ExperienceEntry
packages/utils/contentful/src/types/ExperienceEntry.spec.ts
null
and invalid variants are not accepted.ExperienceMapper.spec.ts
Add test to preserve variant types in ExperienceMapper
packages/utils/javascript/src/lib/ExperienceMapper.spec.ts
Experiement.spec.ts
Add tests for defaulting and validating variants in Experiment
packages/utils/javascript/src/types/Experiement.spec.ts
Experience.spec.ts
Add tests for defaulting and validating variants in Experience
packages/utils/javascript/src/types/Experience.spec.ts
Experience.ts
Fix variant handling in Experience parse functions
packages/utils/javascript/src/types/Experience.ts
parse
andsafeParse
functions to correctly handle variants.Experiment.ts
Fix variant handling in Experiment parse functions
packages/utils/javascript/src/types/Experiment.ts
parse
andsafeParse
functions to correctly handle variants.