-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(headless SSR): validate controller props (#4796)
Return a clear error message if the controller props is undefined  https://coveord.atlassian.net/browse/KIT-3809 --------- Co-authored-by: Frederic Beaudoin <[email protected]> Co-authored-by: Louis Bompart <[email protected]>
- Loading branch information
1 parent
eec0326
commit 3e9f466
Showing
10 changed files
with
119 additions
and
19 deletions.
There are no files selected for viewing
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 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 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
63 changes: 63 additions & 0 deletions
63
packages/headless/src/controllers/commerce/context/headless-context.ssr.test.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import {SSRCommerceEngine} from '../../../app/commerce-ssr-engine/factories/build-factory.js'; | ||
import {buildMockCommerceState} from '../../../test/mock-commerce-state.js'; | ||
import {buildMockSSRCommerceEngine} from '../../../test/mock-engine-v2.js'; | ||
import {MissingControllerProps} from '../../../utils/errors.js'; | ||
import {buildContext, ContextOptions, Context} from './headless-context.js'; | ||
import {ContextDefinition, defineContext} from './headless-context.ssr.js'; | ||
|
||
vi.mock('./headless-context'); | ||
const buildContextMock = vi.mocked(buildContext); | ||
|
||
describe('define commerce context', () => { | ||
const options: ContextOptions = { | ||
language: 'en', | ||
country: 'us', | ||
currency: 'USD', | ||
view: { | ||
url: 'https://example.org', | ||
}, | ||
}; | ||
let contextDefinition: ContextDefinition; | ||
|
||
beforeEach(() => { | ||
buildContextMock.mockReturnValue({} as Context); | ||
contextDefinition = defineContext(); | ||
}); | ||
|
||
afterEach(() => { | ||
buildContextMock.mockClear(); | ||
}); | ||
|
||
it('defineContext returns the proper type', () => { | ||
expect(contextDefinition).toMatchObject<ContextDefinition>({ | ||
buildWithProps: expect.any(Function), | ||
listing: true, | ||
search: true, | ||
standalone: true, | ||
recommendation: true, | ||
}); | ||
}); | ||
|
||
it('buildWithProps should pass its parameters to the buildContext', () => { | ||
const engine: SSRCommerceEngine = buildMockSSRCommerceEngine({ | ||
...buildMockCommerceState(), | ||
commerceContext: {...options}, | ||
}); | ||
|
||
contextDefinition.buildWithProps(engine, options); | ||
|
||
expect(buildContextMock).toBeCalledWith(engine, {options}); | ||
}); | ||
|
||
it('should throw when props is undefined', () => { | ||
const engine: SSRCommerceEngine = buildMockSSRCommerceEngine({ | ||
...buildMockCommerceState(), | ||
commerceContext: {...options}, | ||
}); | ||
const props = undefined as unknown as ContextOptions; | ||
|
||
expect(() => { | ||
contextDefinition.buildWithProps(engine, props); | ||
}).toThrow(MissingControllerProps); | ||
}); | ||
}); |
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 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 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 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 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 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