storj/web/satellite/tests/unit/store/buckets.spec.ts

98 lines
3.0 KiB
TypeScript
Raw Normal View History

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
import Vuex from 'vuex';
import { createLocalVue } from '@vue/test-utils';
2019-09-09 11:33:39 +01:00
import { BucketsApiGql } from '@/api/buckets';
2019-09-09 11:33:39 +01:00
import { ProjectsApiGql } from '@/api/projects';
import { BUCKET_ACTIONS, makeBucketsModule } from '@/store/modules/buckets';
import { makeProjectsModule } from '@/store/modules/projects';
import { Bucket, BucketCursor, BucketPage } from '@/types/buckets';
import { Project } from '@/types/projects';
const Vue = createLocalVue();
const bucketsApi = new BucketsApiGql();
const bucketsModule = makeBucketsModule(bucketsApi);
const { FETCH, SET_SEARCH, CLEAR } = BUCKET_ACTIONS;
const projectsApi = new ProjectsApiGql();
const projectsModule = makeProjectsModule(projectsApi);
const selectedProject = new Project();
selectedProject.id = '1';
projectsModule.state.selectedProject = selectedProject;
Vue.use(Vuex);
const store = new Vuex.Store<{
projectsModule: typeof projectsModule.state,
bucketsModule: typeof bucketsModule.state,
}>({ modules: { projectsModule, bucketsModule } });
const state = store.state.bucketsModule;
const bucket = new Bucket('test', 10, 10, 1, 1, new Date(), new Date());
const page: BucketPage = { buckets: [bucket], currentPage: 1, pageCount: 1, offset: 0, limit: 7, search: 'test', totalCount: 1 };
describe('actions', () => {
beforeEach(() => {
jest.resetAllMocks();
});
it('success fetch buckets', async () => {
jest.spyOn(bucketsApi, 'get').mockReturnValue(
Promise.resolve(page),
);
await store.dispatch(FETCH, 1);
expect(state.page).toEqual(page);
expect(state.cursor.page).toEqual(1);
});
it('fetch throws an error when api call fails', async () => {
jest.spyOn(bucketsApi, 'get').mockImplementation(() => { throw new Error(); });
try {
await store.dispatch(FETCH, 1);
} catch (error) {
expect(state.page).toEqual(page);
}
});
it('success set search buckets', () => {
store.dispatch(SET_SEARCH, 'test');
expect(state.cursor.search).toMatch('test');
});
it('success clear', () => {
store.dispatch(CLEAR);
expect(state.cursor).toEqual(new BucketCursor('', 7, 1));
expect(state.page).toEqual(new BucketPage([], '', 7, 0, 1, 1, 0));
});
});
describe('getters', () => {
const page: BucketPage = { buckets: [bucket], currentPage: 1, pageCount: 1, offset: 0, limit: 7, search: 'test', totalCount: 1 };
it('page of buckets', async () => {
jest.spyOn(bucketsApi, 'get').mockReturnValue(
Promise.resolve(page),
);
await store.dispatch(FETCH, 1);
const storePage = store.getters.page;
expect(storePage).toEqual(page);
});
it('cursor of buckets', () => {
store.dispatch(CLEAR);
const cursor = store.getters.cursor;
expect(cursor).toEqual(new BucketCursor('', 7, 1));
});
});