-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move plan request builder to its own file and add tests
- Loading branch information
1 parent
d91729d
commit 46d70ce
Showing
4 changed files
with
151 additions
and
27 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
...ges/headless/src/features/commerce/standalone-search-box-set/plan-request-builder.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,107 @@ | ||
import {QuerySuggestRequest} from '../../../api/commerce/search/query-suggest/query-suggest-request.js'; | ||
import { | ||
defaultNodeJSNavigatorContextProvider, | ||
NavigatorContext, | ||
} from '../../../app/navigatorContextProvider.js'; | ||
import {buildMockBaseCommerceAPIRequest} from '../../../test/mock-commerce-api-request.js'; | ||
import {buildBaseCommerceAPIRequest} from '../common/base-commerce-api-request-builder.js'; | ||
import {getConfigurationInitialState} from '../configuration/configuration-state.js'; | ||
import {getCartInitialState} from '../context/cart/cart-state.js'; | ||
import {getContextInitialState} from '../context/context-state.js'; | ||
import {getCommerceQueryInitialState} from '../query/query-state.js'; | ||
import { | ||
buildPlanRequest, | ||
StateNeededForPlanCommerceAPIRequest, | ||
} from './plan-request-builder.js'; | ||
|
||
vi.mock('../common/base-commerce-api-request-builder.js'); | ||
|
||
describe('#buildPlanRequest', () => { | ||
let state: StateNeededForPlanCommerceAPIRequest; | ||
let navigatorContext: NavigatorContext; | ||
let request: QuerySuggestRequest; | ||
|
||
const baseRequest = buildMockBaseCommerceAPIRequest(); | ||
|
||
const setState = ( | ||
configuration?: Partial<StateNeededForPlanCommerceAPIRequest> | ||
) => { | ||
state = { | ||
configuration: getConfigurationInitialState(), | ||
cart: getCartInitialState(), | ||
commerceContext: getContextInitialState(), | ||
commerceQuery: getCommerceQueryInitialState(), | ||
...configuration, | ||
}; | ||
}; | ||
|
||
const setNavigatorContext = (configuration?: Partial<NavigatorContext>) => { | ||
navigatorContext = { | ||
...defaultNodeJSNavigatorContextProvider(), | ||
...configuration, | ||
}; | ||
}; | ||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
vi.mocked(buildBaseCommerceAPIRequest).mockReturnValue(baseRequest); | ||
setState(); | ||
setNavigatorContext(); | ||
}); | ||
|
||
it('sets base properties, except #context.capture, to the value returned by #buildBaseCommerceAPIRequest(#state, #navigatorContext)', () => { | ||
const {query, page, perPage, ...restOfPlanRequest} = buildPlanRequest( | ||
state, | ||
navigatorContext | ||
); | ||
|
||
const {capture: planRequestCapture, ...restOfPlanRequestContext} = | ||
restOfPlanRequest.context; | ||
const basePropertiesWithoutCapture = { | ||
...restOfPlanRequest, | ||
context: {...restOfPlanRequestContext}, | ||
}; | ||
|
||
const {capture: baseRequestCapture, ...restOfBaseRequestContext} = | ||
baseRequest.context; | ||
const baseRequestWithoutCapture = { | ||
...baseRequest, | ||
context: {...restOfBaseRequestContext}, | ||
}; | ||
|
||
expect(buildBaseCommerceAPIRequest).toHaveBeenCalledWith( | ||
state, | ||
navigatorContext | ||
); | ||
expect(basePropertiesWithoutCapture).toEqual(baseRequestWithoutCapture); | ||
}); | ||
|
||
it('sets #context.capture to false', () => { | ||
const {context} = buildPlanRequest(state, navigatorContext); | ||
|
||
expect(context.capture).toBe(false); | ||
}); | ||
|
||
it('sets #page to 0', () => { | ||
const {page} = buildPlanRequest(state, navigatorContext); | ||
|
||
expect(page).toBe(0); | ||
}); | ||
|
||
it('sets #perPage to 1', () => { | ||
const {perPage} = buildPlanRequest(state, navigatorContext); | ||
|
||
expect(perPage).toBe(0); | ||
}); | ||
|
||
it('sets #query to #state.commerceQuery.query', () => { | ||
const query = 'some-query'; | ||
setState({ | ||
commerceQuery: { | ||
query, | ||
}, | ||
}); | ||
request = buildPlanRequest(state, navigatorContext); | ||
|
||
expect(request.query).toBe(query); | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
packages/headless/src/features/commerce/standalone-search-box-set/plan-request-builder.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,37 @@ | ||
import { | ||
PageParam, | ||
PerPageParam, | ||
QueryParam, | ||
} from '../../../api/commerce/commerce-api-params.js'; | ||
import {BaseCommerceAPIRequest} from '../../../api/commerce/common/request.js'; | ||
import {NavigatorContext} from '../../../app/navigatorContextProvider.js'; | ||
import {CommerceQuerySection} from '../../../state/state-sections.js'; | ||
import { | ||
buildBaseCommerceAPIRequest, | ||
StateNeededForBaseCommerceAPIRequest, | ||
} from '../common/base-commerce-api-request-builder.js'; | ||
|
||
export type StateNeededForPlanCommerceAPIRequest = | ||
StateNeededForBaseCommerceAPIRequest & CommerceQuerySection; | ||
|
||
export type CommercePlanRequest = BaseCommerceAPIRequest & | ||
QueryParam & | ||
PageParam & | ||
PerPageParam; | ||
|
||
export const buildPlanRequest = ( | ||
state: StateNeededForPlanCommerceAPIRequest, | ||
navigatorContext: NavigatorContext | ||
): CommercePlanRequest => { | ||
const baseRequest = buildBaseCommerceAPIRequest(state, navigatorContext); | ||
return { | ||
...baseRequest, | ||
context: { | ||
...baseRequest.context, | ||
capture: false, | ||
}, | ||
page: 0, | ||
perPage: 1, | ||
query: state.commerceQuery.query, | ||
}; | ||
}; |
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