diff options
Diffstat (limited to 'src/stores')
| -rw-r--r-- | src/stores/forums.js | 2 | ||||
| -rw-r--r-- | src/stores/forums.test.js | 101 |
2 files changed, 102 insertions, 1 deletions
diff --git a/src/stores/forums.js b/src/stores/forums.js index 583b0d3..39822a3 100644 --- a/src/stores/forums.js +++ b/src/stores/forums.js @@ -1,4 +1,4 @@ import { store } from './apollo'; import { GET_FORUMS } from '$/data/queries'; -export const forums = store({ key: 'forums', query: GET_FORUMS, initialValue: [] }); +export const getForums = () => store({ key: 'forums', query: GET_FORUMS, initialValue: [] }); diff --git a/src/stores/forums.test.js b/src/stores/forums.test.js new file mode 100644 index 0000000..25293c8 --- /dev/null +++ b/src/stores/forums.test.js @@ -0,0 +1,101 @@ +import { GraphQLInteraction, Pact, Matchers } from '@pact-foundation/pact'; +import { resolve } from 'path'; + +import { resolveAfter } from '$/utils/resolve_after'; + +import { act } from '@testing-library/svelte'; + +const { eachLike, like } = Matchers; + +jest.mock('$/config/config.js'); + +import { getForums } from './forums'; + +const internals = { + provider: null +}; + +describe('Forum store pact', () => { + + beforeAll(async () => { + + internals.provider = new Pact({ + port: 1234, + dir: resolve(process.cwd(), 'pacts'), + consumer: 'ForumsStore', + provider: 'ForumAPIServer' + }); + + await internals.provider.setup(); + }); + + afterEach(() => internals.provider.verify()); + afterAll(() => internals.provider.finalize()); + + describe('there are forums', () => { + + beforeAll(async () => { + + const forumQuery = new GraphQLInteraction() + .uponReceiving('a request to list the forums') + .withRequest({ + path: '/graphql', + method: 'POST' + }) + .withOperation('GetForums') + .withQuery( + `query GetForums { + forums { + id + glyph + label + position + __typename + } + }` + ) + .withVariables({}) + .willRespondWith({ + status: 200, + headers: { + 'Content-Type': 'application/json; charset=utf-8' + }, + body: { + data: { + forums: eachLike({ + id: like('butter'), + glyph: like('⌘'), + label: like('test_forums.butter'), + position: like(1) + }) + } + } + }); + return await internals.provider.addInteraction(forumQuery); + }); + + test('it returns the forums', async () => { + + const forums = getForums(); + const { counter, promise: resolveAfterTwo } = resolveAfter(2); + let response = null; + forums.subscribe((forumsValue) => { + + response = forumsValue; + counter(); + }); + expect(response.data).toEqual(expect.arrayContaining([])); + expect(response.loading).toBe(true); + expect(response.error).toBe(undefined); + await resolveAfterTwo; + expect(response.data).toEqual(expect.arrayContaining([{ + id: 'butter', + glyph: '⌘', + label: 'test_forums.butter', + position: 1 + }])); + expect(response.loading).toBe(false); + expect(response.error).toBe(undefined); + }); + }); +}); |