// @ts-ignore import { fetchMock } from 'fetch-mock'; import { cloneDeep } from 'lodash'; import RootState from '../types/RootStateModel'; import { CmsStore, storeConfig } from '../index'; import { TypedVueRouter as VueRouter } from '../../tools/vue-router' import { Page, Version } from '../../types/api'; const router = new VueRouter(); export const page: Page = { $type: 'type', id: 'page1', title: 'page', path: '/', name: 'pageName', itemName: 'itemName', itemDisplayName: 'itemDisplayName', parentId: null, isChild: false, allowNew: false, children: [], isSingleton: false, url: '', createdBy: null, modifiedBy: null, language: null, languageRef: null, translations: [] }; const allPages: Page[] = [ { ...page, id: 'page1', }, { ...page, id: 'page2', } ]; const publishedPages = [ { pageId: 'page1', publishedVersionId: 'version1' }, { pageId: 'page2', publishedVersionId: 'version2' }, ]; const pageTree = allPages.map(p => ({ ...p, path: '/', children: [] })); export const pageVersion: Version = { $type: 'type', id: '1', name: 'test', metaTitle: 'test', metaDescription: null, isPublishedOrSchedule: false, metaFollow: false, metaIndex: false, showAsOneSection: false, showPostFix: false, created: '2019-01-01T00:00:00.000Z', modified: '2019-01-01T00:00:00.000Z', publishDate: null, depublishDate: null, page, pageId: 'page1', canonical: null }; const schema = { properties: {}, oneOf: [] }; const mockAllPagesFetch = (status = 200, body: any = allPages) => fetchMock.get(/api\/pages\/all/, { status, body }); const mockPublishedPagesFetch = (status = 200, body: any = publishedPages) => fetchMock.get(/api\/pages\/published/, { status, body }); const mockSchemaFetch = (status = 200, body: any = schema) => fetchMock.get(/api\/versions\/schema/, { status, body }); describe('Page store', () => { afterEach(() => { fetchMock.restore(); }); function createLocalStore(config = storeConfig): CmsStore { return new CmsStore(cloneDeep(config)); } it('should fetch all pages and schema on initialize', async () => { const store = createLocalStore(); const dispatchSpy = jest.spyOn(store, 'dispatch').mockImplementation(jest.fn()); expect(dispatchSpy).not.toHaveBeenCalled(); await store.initialize(router); expect(dispatchSpy).toHaveBeenCalledWith('pages/fetchAllPages', undefined); expect(dispatchSpy).toHaveBeenCalledWith('pages/fetchSchema', undefined); }); it('should fetch all pages and commit them to the store', async () => { mockAllPagesFetch(); mockPublishedPagesFetch(); const store = createLocalStore(); const commitSpy = jest.spyOn(store, 'commit').mockImplementation(jest.fn()); expect(commitSpy).not.toHaveBeenCalled(); await store.dispatch('pages/fetchAllPages'); expect(commitSpy).toHaveBeenCalledWith('pages/allPages', allPages, undefined); }); it('should update loading state on fetching all pages', async () => { mockAllPagesFetch(); mockPublishedPagesFetch(); const store = createLocalStore(); const commitSpy = jest.spyOn(store, 'commit').mockImplementation(jest.fn()); expect(commitSpy).not.toHaveBeenCalled(); expect(store.state.pages.isLoadingPages).toBeFalsy(); await store.dispatch('pages/fetchAllPages'); expect(commitSpy).toHaveBeenCalledWith('pages/isLoadingPages', true, undefined); expect(commitSpy).toHaveBeenCalledWith('pages/isLoadingPages', false, undefined); expect(store.state.pages.isLoadingPages).toBeFalsy(); }); it('should set loading state to false after fetching published pages', async () => { mockPublishedPagesFetch(); const store = createLocalStore(); const commitSpy = jest.spyOn(store, 'commit').mockImplementation(jest.fn()); await store.dispatch('pages/fetchPublishedPageIds'); expect(commitSpy).toHaveBeenCalledWith('pages/isLoadingPublishedPages', false, undefined); }); it('should commit fetched published pages to string array', async () => { fetchMock.get(/api\/pages\/published/, { status: 200, body: publishedPages, }); mockAllPagesFetch(); const store = createLocalStore(); await store.dispatch('pages/fetchPublishedPageIds'); const publishedPageIds = publishedPages.map(p => p.pageId); expect(store.state.pages.publishedPageIds).toEqual(publishedPageIds); }); it('should return pagetree items from pagetree getter', () => { const store = createLocalStore(); store.state.pages.allPages = allPages; expect(store.getters['pages/pageTree']).toEqual(pageTree); }); it('should fetch schema and commit schema to state', async () => { mockSchemaFetch(); const store = createLocalStore(); const commitSpy = jest.spyOn(store, 'commit').mockImplementation(jest.fn()); await store.dispatch('pages/fetchSchema'); expect(commitSpy).toHaveBeenCalledWith('pages/schema', schema, undefined); }); it('should update loading state on fetching schema', async () => { mockSchemaFetch(); const store = createLocalStore(); const commitSpy = jest.spyOn(store, 'commit').mockImplementation(jest.fn()); expect(commitSpy).not.toHaveBeenCalled(); expect(store.state.pages.isLoadingPages).toBeFalsy(); await store.dispatch('pages/fetchSchema'); expect(commitSpy).toHaveBeenCalledWith('pages/isLoadingPages', true, undefined); expect(commitSpy).toHaveBeenCalledWith('pages/isLoadingPages', false, undefined); expect(store.state.pages.isLoadingPages).toBeFalsy(); }); it('should create and return a page version', async () => { fetchMock.post(/api\/versions\/create/, { status: 200, body: pageVersion, }); const config = storeConfig; storeConfig.modules!.pages.actions!.fetchAllPages = jest.fn(); const store = createLocalStore(config); const createdVersion = await store.dispatch('pages/create', {}); expect(pageVersion).toEqual(createdVersion); }); it('should create from copy and return a page version', async () => { fetchMock.post(/api\/versions\/create-from-copy/, { status: 200, body: pageVersion, }); const config = storeConfig; storeConfig.modules!.pages.actions!.fetchAllPages = jest.fn(); const store = createLocalStore(config); const createdVersion = await store.dispatch('pages/createFromCopy', {}); expect(pageVersion).toEqual(createdVersion); expect(storeConfig.modules!.pages.actions!.fetchAllPages).toHaveBeenCalled(); }); it('should update and return a page version', async () => { fetchMock.put(/api\/versions\/update/, { status: 200, body: pageVersion, }); const config = storeConfig; storeConfig.modules!.pages.actions!.fetchAllPages = jest.fn(); const store = createLocalStore(config); const updatedVersion = await store.dispatch('pages/updateVersion', pageVersion); expect(pageVersion).toEqual(updatedVersion); expect(storeConfig.modules!.pages.actions!.fetchAllPages).toHaveBeenCalled(); }); it('should publish and return a page version', async () => { fetchMock.post(`/api/versions/publish/${pageVersion.id}`, { status: 200, body: pageVersion, }); const config = storeConfig; storeConfig.modules!.pages.actions!.fetchAllPages = jest.fn(); const store = createLocalStore(config); const publishedVersion = await store.dispatch('pages/publishVersion', pageVersion); expect(pageVersion).toEqual(publishedVersion); expect(storeConfig.modules!.pages.actions!.fetchAllPages).toHaveBeenCalled(); }); it('should depublish and return a page version', async () => { fetchMock.post(`/api/versions/depublish/${pageVersion.id}`, { status: 200, body: pageVersion, }); const config = storeConfig; storeConfig.modules!.pages.actions!.fetchAllPages = jest.fn(); const store = createLocalStore(config); const depublishedVersion = await store.dispatch('pages/depublishVersion', pageVersion); expect(pageVersion).toEqual(depublishedVersion); expect(storeConfig.modules!.pages.actions!.fetchAllPages).toHaveBeenCalled(); }); it('should delete and return true', async () => { fetchMock.delete(`/api/versions/${pageVersion.id}`, { status: 200, }); const config = storeConfig; storeConfig.modules!.pages.actions!.fetchAllPages = jest.fn(); const store = createLocalStore(config); const deleted = await store.dispatch('pages/deleteVersion', pageVersion); expect(storeConfig.modules!.pages.actions!.fetchAllPages).toHaveBeenCalled(); expect(deleted).toEqual(true); }); it('should move and return a page', async () => { const newParentId = '1'; fetchMock.post(`/api/pages/${page.id}/move?newParentId=${newParentId}`, { status: 200, body: page, }); const config = storeConfig; storeConfig.modules!.pages.actions!.fetchAllPages = jest.fn(); const store = createLocalStore(config); const movedPage = await store.dispatch('pages/move', { page, newParentId }); expect(page).toEqual(movedPage); expect(storeConfig.modules!.pages.actions!.fetchAllPages).toHaveBeenCalled(); }); it('should rename and return a page', async () => { const name = 'page'; fetchMock.post(`/api/pages/${page.id}/rename?name=${name}`, { status: 200, body: page, }); const config = storeConfig; storeConfig.modules!.pages.actions!.fetchAllPages = jest.fn(); const store = createLocalStore(config); const renamedPage = await store.dispatch('pages/rename', { page, name }); expect(page).toEqual(renamedPage); expect(storeConfig.modules!.pages.actions!.fetchAllPages).toHaveBeenCalled(); }); it('should delete a page and return true', async () => { fetchMock.delete(`/api/pages/${page.id}`, { status: 200 }); const config = storeConfig; storeConfig.modules!.pages.actions!.fetchAllPages = jest.fn(); const store = createLocalStore(config); const deleted = await store.dispatch('pages/delete', page); expect(storeConfig.modules!.pages.actions!.fetchAllPages).toHaveBeenCalled(); expect(deleted).toEqual(true); }); });