-
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.
feat(commerce): add core + plp date range facets (#3481)
- Loading branch information
1 parent
b644bdf
commit 19eb84a
Showing
37 changed files
with
1,436 additions
and
265 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
86 changes: 86 additions & 0 deletions
86
...s/headless/src/controllers/commerce/facets/core/date/headless-commerce-date-facet.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,86 @@ | ||
import {CommerceFacetRequest} from '../../../../../features/commerce/facets/facet-set/interfaces/request'; | ||
import {FacetType} from '../../../../../features/commerce/facets/facet-set/interfaces/response'; | ||
import {fetchProductListing} from '../../../../../features/commerce/product-listing/product-listing-actions'; | ||
import { | ||
toggleExcludeDateFacetValue, | ||
toggleSelectDateFacetValue, | ||
} from '../../../../../features/facets/range-facets/date-facet-set/date-facet-actions'; | ||
import {CommerceAppState} from '../../../../../state/commerce-app-state'; | ||
import {MockCommerceEngine, buildMockCommerceEngine} from '../../../../../test'; | ||
import {buildMockCommerceFacetRequest} from '../../../../../test/mock-commerce-facet-request'; | ||
import {buildMockCommerceDateFacetResponse} from '../../../../../test/mock-commerce-facet-response'; | ||
import {buildMockCommerceFacetSlice} from '../../../../../test/mock-commerce-facet-slice'; | ||
import {buildMockCommerceDateFacetValue} from '../../../../../test/mock-commerce-facet-value'; | ||
import {buildMockCommerceState} from '../../../../../test/mock-commerce-state'; | ||
import { | ||
CommerceDateFacet, | ||
CommerceDateFacetOptions, | ||
buildCommerceDateFacet, | ||
} from './headless-commerce-date-facet'; | ||
|
||
describe('CommerceDateFacet', () => { | ||
const facetId: string = 'date_facet_id'; | ||
const type: FacetType = 'dateRange'; | ||
const start = '2023-01-01'; | ||
const end = '2024-01-01'; | ||
let options: CommerceDateFacetOptions; | ||
let state: CommerceAppState; | ||
let engine: MockCommerceEngine; | ||
let facet: CommerceDateFacet; | ||
|
||
function initFacet() { | ||
engine = buildMockCommerceEngine({state}); | ||
facet = buildCommerceDateFacet(engine, options); | ||
} | ||
|
||
function setFacetRequest(config: Partial<CommerceFacetRequest> = {}) { | ||
state.commerceFacetSet[facetId] = buildMockCommerceFacetSlice({ | ||
request: buildMockCommerceFacetRequest({facetId, type, ...config}), | ||
}); | ||
state.productListing.facets = [ | ||
buildMockCommerceDateFacetResponse({facetId}), | ||
]; | ||
} | ||
|
||
beforeEach(() => { | ||
options = { | ||
facetId, | ||
fetchResultsActionCreator: fetchProductListing, | ||
}; | ||
|
||
state = buildMockCommerceState(); | ||
setFacetRequest(); | ||
|
||
initFacet(); | ||
}); | ||
|
||
it('initializes', () => { | ||
expect(facet).toBeTruthy(); | ||
}); | ||
|
||
it('exposes #subscribe method', () => { | ||
expect(facet.subscribe).toBeTruthy(); | ||
}); | ||
|
||
describe('#toggleSelect', () => { | ||
it('dispatches #toggleSelectDateFacetValue', () => { | ||
const facetValue = buildMockCommerceDateFacetValue({start, end}); | ||
facet.toggleSelect(facetValue); | ||
|
||
expect(engine.actions).toContainEqual( | ||
toggleSelectDateFacetValue({facetId, selection: facetValue}) | ||
); | ||
}); | ||
}); | ||
|
||
describe('#toggleExclude', () => { | ||
it('dispatches #toggleExcludeDateFacetValue', () => { | ||
const facetValue = buildMockCommerceDateFacetValue({start, end}); | ||
facet.toggleExclude(facetValue); | ||
|
||
expect(engine.actions).toContainEqual( | ||
toggleExcludeDateFacetValue({facetId, selection: facetValue}) | ||
); | ||
}); | ||
}); | ||
}); |
51 changes: 51 additions & 0 deletions
51
packages/headless/src/controllers/commerce/facets/core/date/headless-commerce-date-facet.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,51 @@ | ||
import {CommerceEngine} from '../../../../../app/commerce-engine/commerce-engine'; | ||
import { | ||
toggleExcludeDateFacetValue, | ||
toggleSelectDateFacetValue, | ||
} from '../../../../../features/facets/range-facets/date-facet-set/date-facet-actions'; | ||
import { | ||
CoreCommerceFacet, | ||
CoreCommerceFacetOptions, | ||
DateFacetValue, | ||
DateRangeRequest, | ||
buildCoreCommerceFacet, | ||
} from '../headless-core-commerce-facet'; | ||
|
||
export type CommerceDateFacetOptions = Omit< | ||
CoreCommerceFacetOptions, | ||
'toggleSelectActionCreator' | 'toggleExcludeActionCreator' | ||
>; | ||
|
||
/** | ||
* The `CommerceDateFacet` controller offers a high-level programming interface for implementing date commerce | ||
* facet UI component. | ||
*/ | ||
export type CommerceDateFacet = CoreCommerceFacet< | ||
DateRangeRequest, | ||
DateFacetValue | ||
>; | ||
|
||
/** | ||
* @internal | ||
* | ||
* **Important:** This initializer is meant for internal use by headless only. | ||
* As an implementer, you must not import or use this initializer directly in your code. | ||
* You will instead interact with `CommerceDateFacet` controller instances through the state of a `FacetGenerator` | ||
* controller. | ||
* | ||
* @param engine - The headless commerce engine. | ||
* @param options - The `CommerceDateFacet` options used internally. | ||
* @returns A `CommerceDateFacet` controller instance. | ||
*/ | ||
export function buildCommerceDateFacet( | ||
engine: CommerceEngine, | ||
options: CommerceDateFacetOptions | ||
): CommerceDateFacet { | ||
return buildCoreCommerceFacet<DateRangeRequest, DateFacetValue>(engine, { | ||
options: { | ||
...options, | ||
toggleSelectActionCreator: toggleSelectDateFacetValue, | ||
toggleExcludeActionCreator: toggleExcludeDateFacetValue, | ||
}, | ||
}); | ||
} |
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
Oops, something went wrong.