import fetchMock from 'fetch-mock'; import { cloneDeep } from 'lodash'; import { afterEach, describe, expect, it, vi } from 'vitest'; import RootState from '../types/RootStateModel'; import { CmsStore, storeConfig } from '../index'; import { sampleAssets } from '../../constants/test-constants'; describe('Asset store', () => { afterEach(() => { fetchMock.restore(); }); function createLocalStore(): CmsStore { return new CmsStore(cloneDeep(storeConfig)); } it('should fetch assets', () => { const store = createLocalStore(); store.dispatch = vi.fn(); expect(store.dispatch).not.toHaveBeenCalled(); store.initialize(); expect(store.dispatch).toHaveBeenCalledWith('assets/fetchItems', undefined); }); it('should fetch assets and put them in a store', async () => { fetchMock.post(/api\/assets\/search/, { status: 200, body: { timestamp: 0, result: sampleAssets } }); const store = createLocalStore(); store.commit = vi.fn(); expect(store.commit).not.toHaveBeenCalled(); await store.dispatch('assets/fetchItems'); expect(store.commit).toHaveBeenCalledWith('assets/isLoading', true, undefined); expect(store.commit).toHaveBeenCalledWith('assets/setLastSearchQueryTimestamp', expect.anything(), undefined); expect(store.commit).toHaveBeenCalledWith('assets/setResult', sampleAssets, undefined); expect(store.commit).toHaveBeenCalledWith('assets/isLoading', false, undefined); }); it('should update the state of assets', async () => { const store = createLocalStore(); store.commit('assets/setAssets', sampleAssets); expect(store.state.assets.allAssets).toEqual(sampleAssets); }); it('should update an existing asset', async () => { const resp = { foo: 'bar' }; fetchMock.put(/api\/assets\/6677ca7c-8f5e-4e9b-bfcb-1a7df7e654f0\/update/, { status: 200, body: resp }); fetchMock.get(/api\/assets\/list/, { status: 200, body: sampleAssets }); fetchMock.post(/api\/assets\/search/, { status: 200, body: { timestamp: 0, result: sampleAssets } }); const store = createLocalStore(); const response = await store.dispatch('assets/updateAsset', sampleAssets[0]); expect(response).toEqual(resp); }); it('should create a new asset', async () => { const resp = { foo: 'bar' }; fetchMock.post(/api\/assets\/create/, { status: 200, body: resp }); fetchMock.get(/api\/assets\/list/, { status: 200, body: sampleAssets }); fetchMock.post(/api\/assets\/search/, { status: 200, body: { timestamp: 0, result: sampleAssets } }); const store = createLocalStore(); const formData = new FormData(); const response = await store.dispatch('assets/createAsset', formData); expect(response).toEqual(resp); }); it('should delete an asset', async () => { fetchMock.delete(/api\/assets\/1234\/delete/, { status: 200, body: null }); fetchMock.get(/api\/assets\/list/, { status: 200, body: sampleAssets }); fetchMock.post(/api\/assets\/search/, { status: 200, body: { timestamp: 0, result: sampleAssets } }); const store = createLocalStore(); const response = await store.dispatch('assets/deleteAsset', 1234); expect(response).toEqual(true); }); });