-
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.
test: test for RP module and doc for related-prompts-list
- Loading branch information
1 parent
f9d5b10
commit 976f943
Showing
4 changed files
with
353 additions
and
0 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
118 changes: 118 additions & 0 deletions
118
packages/x-components/src/x-modules/related-prompts/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,118 @@ | ||
import { mount } from '@vue/test-utils'; | ||
import { Store } from 'vuex'; | ||
import { getRelatedPromptsStub } from '../../../../__stubs__'; | ||
import { getMockedAdapter, installNewXPlugin } from '../../../../__tests__/utils'; | ||
import { SafeStore } from '../../../../store/__tests__/utils'; | ||
import { | ||
RelatedPromptsActions, | ||
RelatedPromptsGetters, | ||
RelatedPromptsMutations, | ||
RelatedPromptsState | ||
} from '../types'; | ||
import { relatedPromptsXStoreModule } from '../module'; | ||
import { resetRelatedPromptsStateWith } from './utils'; | ||
|
||
describe('testing related prompts module actions', () => { | ||
const mockedRelatedPrompts = getRelatedPromptsStub(); | ||
|
||
const adapter = getMockedAdapter({ | ||
relatedPrompts: { relatedPrompts: mockedRelatedPrompts } | ||
}); | ||
|
||
const store: SafeStore< | ||
RelatedPromptsState, | ||
RelatedPromptsGetters, | ||
RelatedPromptsMutations, | ||
RelatedPromptsActions | ||
> = new Store(relatedPromptsXStoreModule as any); | ||
mount( | ||
{}, | ||
{ | ||
global: { | ||
plugins: [installNewXPlugin({ adapter, store })] | ||
} | ||
} | ||
); | ||
|
||
beforeEach(() => { | ||
resetRelatedPromptsStateWith(store); | ||
}); | ||
|
||
describe('fetchRelatedPrompts', () => { | ||
it('should return related prompts', async () => { | ||
resetRelatedPromptsStateWith(store, { | ||
query: 'honeyboo' | ||
}); | ||
|
||
const relatedPrompts = await store.dispatch('fetchRelatedPrompts', store.getters.request); | ||
expect(relatedPrompts).toEqual(mockedRelatedPrompts); | ||
}); | ||
|
||
it('should return `null` if there is not request', async () => { | ||
const relatedPrompts = await store.dispatch('fetchRelatedPrompts', store.getters.request); | ||
expect(relatedPrompts).toBeNull(); | ||
}); | ||
}); | ||
|
||
describe('fetchAndSaveRelatedPrompts', () => { | ||
it('should request and store related prompts in the state', async () => { | ||
resetRelatedPromptsStateWith(store, { | ||
query: 'honeyboo' | ||
}); | ||
|
||
const actionPromise = store.dispatch('fetchAndSaveRelatedPrompts', store.getters.request); | ||
expect(store.state.status).toEqual('loading'); | ||
await actionPromise; | ||
expect(store.state.relatedPrompts).toEqual(mockedRelatedPrompts); | ||
expect(store.state.status).toEqual('success'); | ||
}); | ||
|
||
it('should not clear related prompts in the state if the query is empty', async () => { | ||
resetRelatedPromptsStateWith(store, { relatedPrompts: mockedRelatedPrompts }); | ||
|
||
await store.dispatch('fetchAndSaveRelatedPrompts', store.getters.request); | ||
expect(store.state.relatedPrompts).toEqual(mockedRelatedPrompts); | ||
}); | ||
|
||
it('should cancel the previous request if it is not yet resolved', async () => { | ||
resetRelatedPromptsStateWith(store, { query: 'steak' }); | ||
const initialRelatedPrompts = store.state.relatedPrompts; | ||
adapter.relatedPrompts.mockResolvedValueOnce({ | ||
relatedPrompts: mockedRelatedPrompts.slice(0, 1) | ||
}); | ||
|
||
const firstRequest = store.dispatch('fetchAndSaveRelatedPrompts', store.getters.request); | ||
const secondRequest = store.dispatch('fetchAndSaveRelatedPrompts', store.getters.request); | ||
|
||
await firstRequest; | ||
expect(store.state.status).toEqual('loading'); | ||
expect(store.state.relatedPrompts).toBe(initialRelatedPrompts); | ||
await secondRequest; | ||
expect(store.state.status).toEqual('success'); | ||
expect(store.state.relatedPrompts).toEqual(mockedRelatedPrompts); | ||
}); | ||
|
||
it('should set the status to error when it fails', async () => { | ||
resetRelatedPromptsStateWith(store, { query: 'milk' }); | ||
adapter.relatedPrompts.mockRejectedValueOnce('Generic error'); | ||
const relatedPrompts = store.state.relatedPrompts; | ||
await store.dispatch('fetchAndSaveRelatedPrompts', store.getters.request); | ||
|
||
expect(store.state.relatedPrompts).toBe(relatedPrompts); | ||
expect(store.state.status).toEqual('error'); | ||
}); | ||
}); | ||
|
||
describe('cancelFetchAndSaveRelatedPrompts', () => { | ||
it('should cancel the request and do not modify the stored related prompts', async () => { | ||
resetRelatedPromptsStateWith(store, { query: 'honeyboo' }); | ||
const previousRelatedPrompts = store.state.relatedPrompts; | ||
await Promise.all([ | ||
store.dispatch('fetchAndSaveRelatedPrompts', store.getters.request), | ||
store.dispatch('cancelFetchAndSaveRelatedPrompts') | ||
]); | ||
expect(store.state.relatedPrompts).toEqual(previousRelatedPrompts); | ||
expect(store.state.status).toEqual('success'); | ||
}); | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
packages/x-components/src/x-modules/related-prompts/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,37 @@ | ||
import { RelatedPromptsRequest } from '@empathyco/x-types'; | ||
import { map } from '@empathyco/x-utils'; | ||
import { Store } from 'vuex'; | ||
import { relatedPromptsXStoreModule } from '../module'; | ||
import { RelatedPromptsState } from '../types'; | ||
import { resetRelatedPromptsStateWith } from './utils'; | ||
|
||
describe('testing related prompts module getters', () => { | ||
const gettersKeys = map(relatedPromptsXStoreModule.getters, getter => getter); | ||
const store: Store<RelatedPromptsState> = new Store(relatedPromptsXStoreModule as any); | ||
|
||
beforeEach(() => { | ||
resetRelatedPromptsStateWith(store); | ||
}); | ||
|
||
describe(`${gettersKeys.request} getter`, () => { | ||
it('should return a request object if there is a query', () => { | ||
resetRelatedPromptsStateWith(store, { | ||
query: 'queso', | ||
params: { | ||
catalog: 'es' | ||
} | ||
}); | ||
|
||
expect(store.getters[gettersKeys.request]).toEqual<RelatedPromptsRequest>({ | ||
query: 'queso', | ||
extraParams: { | ||
catalog: 'es' | ||
} | ||
}); | ||
}); | ||
|
||
it('should return null when there is not query', () => { | ||
expect(store.getters[gettersKeys.request]).toBeNull(); | ||
}); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
packages/x-components/src/x-modules/related-prompts/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,21 @@ | ||
import { DeepPartial } from '@empathyco/x-utils'; | ||
import { Store } from 'vuex'; | ||
import { resetStoreModuleState } from '../../../../__tests__/utils'; | ||
import { relatedPromptsXStoreModule } from '../module'; | ||
import { RelatedPromptsState } from '../types'; | ||
|
||
/** | ||
* Reset related prompt module state with its original state and the partial state passes as | ||
* parameter. | ||
* | ||
* @param store - Related prompt store state. | ||
* @param state - Partial related prompt store state to be replaced. | ||
* | ||
* @internal | ||
*/ | ||
export function resetRelatedPromptsStateWith( | ||
store: Store<RelatedPromptsState>, | ||
state?: DeepPartial<RelatedPromptsState> | ||
): void { | ||
resetStoreModuleState<RelatedPromptsState>(store, relatedPromptsXStoreModule.state(), state); | ||
} |