-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create experience controls module tests (#1303)
- Loading branch information
Showing
8 changed files
with
286 additions
and
3 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
packages/x-components/src/__stubs__/experience-controls-stubs.factory.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,24 @@ | ||
import { ExperienceControlsResponse } from '@empathyco/x-types'; | ||
|
||
/** | ||
* Creates a an experience controls response stub. | ||
* | ||
* @returns An experience controls stub. | ||
* | ||
* @internal | ||
*/ | ||
export function getExperienceControlsStub(): ExperienceControlsResponse { | ||
return createExperienceControlsStub(); | ||
} | ||
|
||
/** | ||
* Creates an experience controls response. | ||
* | ||
* @returns An experience controls response. | ||
*/ | ||
export function createExperienceControlsStub(): ExperienceControlsResponse { | ||
return { | ||
controls: { numberOfCarousels: 10, resultsPerCarousels: 21 }, | ||
events: { ColumnsNumberProvided: 6 } | ||
}; | ||
} |
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
61 changes: 61 additions & 0 deletions
61
...onents/src/x-modules/experience-controls/components/__tests__/experience-controls.spec.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,61 @@ | ||
import { mount, Wrapper } from '@vue/test-utils'; | ||
import Vue from 'vue'; | ||
import { XPlugin } from '../../../../plugins/index'; | ||
import { installNewXPlugin } from '../../../../__tests__/utils'; | ||
import { experienceControlsXModule } from '../../x-module'; | ||
import { getXComponentXModuleName, isXComponent } from '../../../../components/index'; | ||
import ExperienceControls from '../experience-controls.vue'; | ||
|
||
function renderExperienceControls(): RenderExperienceControlsApi { | ||
const [, localVue] = installNewXPlugin(); | ||
XPlugin.registerXModule(experienceControlsXModule); | ||
|
||
const wrapper = mount(ExperienceControls, { | ||
localVue | ||
}); | ||
|
||
return { | ||
wrapper | ||
}; | ||
} | ||
|
||
describe('testing experience controls component', () => { | ||
it('is an XComponent which has an XModule', () => { | ||
const { wrapper } = renderExperienceControls(); | ||
expect(isXComponent(wrapper.vm)).toEqual(true); | ||
expect(getXComponentXModuleName(wrapper.vm)).toEqual('experienceControls'); | ||
}); | ||
|
||
// eslint-disable-next-line max-len | ||
it('listens to the event ExperienceControlsEventsChanged and emits the events on the payload', () => { | ||
const { wrapper } = renderExperienceControls(); | ||
|
||
const eventsFromExperienceControls = { | ||
ExtraParamsProvided: { | ||
warehouse: 'Magrathea' | ||
}, | ||
SortChanged: 'price:desc' | ||
}; | ||
|
||
const extraParamsProvidedListener = jest.fn(); | ||
wrapper.vm.$x.on('ExtraParamsProvided').subscribe(extraParamsProvidedListener); | ||
|
||
const sortChangedListener = jest.fn(); | ||
wrapper.vm.$x.on('SortChanged').subscribe(sortChangedListener); | ||
|
||
wrapper.vm.$x.emit('ExperienceControlsEventsChanged', eventsFromExperienceControls); | ||
|
||
expect(extraParamsProvidedListener).toHaveBeenCalledTimes(1); | ||
expect(extraParamsProvidedListener).toHaveBeenCalledWith({ | ||
warehouse: 'Magrathea' | ||
}); | ||
|
||
expect(sortChangedListener).toHaveBeenCalledTimes(1); | ||
expect(sortChangedListener).toHaveBeenCalledWith('price:desc'); | ||
}); | ||
}); | ||
|
||
interface RenderExperienceControlsApi { | ||
/** The wrapper for the experience controls component. */ | ||
wrapper: Wrapper<Vue>; | ||
} |
87 changes: 87 additions & 0 deletions
87
packages/x-components/src/x-modules/experience-controls/store/__tests__/actions.spec.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,87 @@ | ||
import Vuex from 'vuex'; | ||
import { createLocalVue } from '@vue/test-utils'; | ||
import { getMockedAdapter, installNewXPlugin } from '../../../../__tests__/utils'; | ||
import { getExperienceControlsStub } from '../../../../__stubs__/experience-controls-stubs.factory'; | ||
import { createExperienceControlsStore, resetExperienceControlsStateWith } from './utils'; | ||
|
||
describe('testing experience controls module actions', () => { | ||
const mockedResponse = getExperienceControlsStub(); | ||
|
||
const adapter = getMockedAdapter({ | ||
experienceControls: mockedResponse | ||
}); | ||
|
||
const localVue = createLocalVue(); | ||
localVue.config.productionTip = false; // Silent production console messages. | ||
localVue.use(Vuex); | ||
const store = createExperienceControlsStore(); | ||
installNewXPlugin({ adapter, store }, localVue); | ||
|
||
beforeEach(() => { | ||
resetExperienceControlsStateWith(store); | ||
}); | ||
|
||
describe('fetchControls', () => { | ||
it('should return experience controls response', async () => { | ||
const experienceControls = await store.dispatch( | ||
'fetchExperienceControlsResponse', | ||
store.getters.experienceControlsRequest | ||
); | ||
expect(experienceControls).toEqual(mockedResponse); | ||
}); | ||
}); | ||
|
||
describe('fetchAndSaveControls', () => { | ||
it('should request and store controls and events in the state', async () => { | ||
const actionPromise = store.dispatch( | ||
'fetchAndSaveExperienceControlsResponse', | ||
store.getters.experienceControlsRequest | ||
); | ||
expect(store.state.status).toEqual('loading'); | ||
await actionPromise; | ||
|
||
expect(store.state.controls).toEqual(mockedResponse.controls); | ||
expect(store.state.events).toEqual(mockedResponse.events); | ||
expect(store.state.status).toEqual('success'); | ||
}); | ||
|
||
it('should cancel the previous request if it is not yet resolved', async () => { | ||
const initialExperienceControls = store.state.controls; | ||
adapter.experienceControls.mockResolvedValueOnce(mockedResponse); | ||
|
||
const firstRequest = store.dispatch( | ||
'fetchAndSaveExperienceControlsResponse', | ||
store.getters.experienceControlsRequest | ||
); | ||
const secondRequest = store.dispatch( | ||
'fetchAndSaveExperienceControlsResponse', | ||
store.getters.experienceControlsRequest | ||
); | ||
|
||
await firstRequest; | ||
expect(store.state.status).toEqual('loading'); | ||
expect(store.state.controls).toBe(initialExperienceControls); | ||
await secondRequest; | ||
expect(store.state.status).toEqual('success'); | ||
expect(store.state.controls).toEqual(mockedResponse.controls); | ||
}); | ||
}); | ||
|
||
describe('cancelFetchAndSaveControls', () => { | ||
it('should cancel the request and do not modify the stored controls', async () => { | ||
resetExperienceControlsStateWith(store, { | ||
controls: { numberOfCarousels: 20, resultsPerCarousels: 6 } | ||
}); | ||
const previousControls = store.state.controls; | ||
await Promise.all([ | ||
store.dispatch( | ||
'fetchAndSaveExperienceControlsResponse', | ||
store.getters.experienceControlsRequest | ||
), | ||
store.dispatch('cancelFetchAndSaveControls') | ||
]); | ||
expect(store.state.controls).toEqual(previousControls); | ||
expect(store.state.status).toEqual('success'); | ||
}); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
packages/x-components/src/x-modules/experience-controls/store/__tests__/getters.spec.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,31 @@ | ||
import Vuex from 'vuex'; | ||
import { ExperienceControlsRequest } from '@empathyco/x-types'; | ||
import { createLocalVue } from '@vue/test-utils'; | ||
import { createExperienceControlsStore, resetExperienceControlsStateWith } from './utils'; | ||
|
||
describe('testing experience controls module getters', () => { | ||
const localVue = createLocalVue(); | ||
localVue.config.productionTip = false; | ||
localVue.use(Vuex); | ||
const store = createExperienceControlsStore(); | ||
|
||
beforeEach(() => { | ||
resetExperienceControlsStateWith(store); | ||
}); | ||
|
||
describe(`request getter`, () => { | ||
it('should return a request object', () => { | ||
resetExperienceControlsStateWith(store, { | ||
params: { | ||
store: 'es' | ||
} | ||
}); | ||
|
||
expect(store.getters.experienceControlsRequest).toEqual<ExperienceControlsRequest>({ | ||
extraParams: { | ||
store: 'es' | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
packages/x-components/src/x-modules/experience-controls/store/__tests__/utils.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,45 @@ | ||
import { DeepPartial } from '@empathyco/x-utils'; | ||
import { Store } from 'vuex'; | ||
import { resetStoreModuleState } from '../../../../__tests__/utils'; | ||
import { experienceControlsXStoreModule } from '../module'; | ||
import { | ||
ExperienceControlsActions, | ||
ExperienceControlsGetters, | ||
ExperienceControlsMutations, | ||
ExperienceControlsState | ||
} from '../types'; | ||
import { SafeStore } from '../../../../store/__tests__/utils'; | ||
|
||
/** | ||
* Resets the experience controls module state, optionally modifying its default values. | ||
* | ||
* @param store - Experience controls store state. | ||
* @param state - Partial experience controls store state to replace the original one. | ||
* | ||
* @internal | ||
*/ | ||
export function resetExperienceControlsStateWith( | ||
store: Store<ExperienceControlsState>, | ||
state?: DeepPartial<ExperienceControlsState> | ||
): void { | ||
resetStoreModuleState(store, experienceControlsXStoreModule.state(), state); | ||
} | ||
|
||
/** | ||
* Creates an experience controls store with the state passed as parameter. | ||
* | ||
* @returns Store - The new store created. | ||
* | ||
* @internal | ||
*/ | ||
export function createExperienceControlsStore(): Store<ExperienceControlsState> { | ||
const store: SafeStore< | ||
ExperienceControlsState, | ||
ExperienceControlsGetters, | ||
ExperienceControlsMutations, | ||
ExperienceControlsActions | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
> = new Store(experienceControlsXStoreModule as any); | ||
|
||
return store; | ||
} |
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