2019-08-21 15:07:49 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
import Vuex from 'vuex';
|
2019-09-09 11:33:39 +01:00
|
|
|
|
2019-08-21 15:07:49 +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';
|
2019-08-21 15:07:49 +01:00
|
|
|
import { makeProjectsModule } from '@/store/modules/projects';
|
|
|
|
import { Bucket, BucketCursor, BucketPage } from '@/types/buckets';
|
|
|
|
import { Project } from '@/types/projects';
|
2019-09-09 11:33:39 +01:00
|
|
|
import { createLocalVue } from '@vue/test-utils';
|
2019-08-21 15:07:49 +01:00
|
|
|
|
|
|
|
const Vue = createLocalVue();
|
|
|
|
const bucketsApi = new BucketsApiGql();
|
|
|
|
const bucketsModule = makeBucketsModule(bucketsApi);
|
|
|
|
const { FETCH, SET_SEARCH, CLEAR } = BUCKET_ACTIONS;
|
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
const projectsApi = new ProjectsApiGql();
|
|
|
|
const projectsModule = makeProjectsModule(projectsApi);
|
2019-08-21 15:07:49 +01:00
|
|
|
const selectedProject = new Project();
|
|
|
|
selectedProject.id = '1';
|
|
|
|
projectsModule.state.selectedProject = selectedProject;
|
|
|
|
|
|
|
|
Vue.use(Vuex);
|
|
|
|
|
|
|
|
const store = new Vuex.Store({modules: { projectsModule, bucketsModule } });
|
|
|
|
const state = (store.state as any).bucketsModule;
|
|
|
|
const bucket = new Bucket('test', 10, 10, 1, new Date(), new Date());
|
2019-09-09 14:44:54 +01:00
|
|
|
const page: BucketPage = { buckets: [bucket], currentPage: 1, pageCount: 1, offset: 0, limit: 7, search: 'test', totalCount: 1 };
|
2019-08-21 15:07:49 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-09-09 14:44:54 +01:00
|
|
|
expect(state.cursor).toEqual(new BucketCursor('', 7, 1));
|
|
|
|
expect(state.page).toEqual(new BucketPage([], '', 7, 0, 1, 1, 0));
|
2019-08-21 15:07:49 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getters', () => {
|
2019-09-09 14:44:54 +01:00
|
|
|
const page: BucketPage = { buckets: [bucket], currentPage: 1, pageCount: 1, offset: 0, limit: 7, search: 'test', totalCount: 1 };
|
2019-08-21 15:07:49 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2019-09-09 14:44:54 +01:00
|
|
|
expect(cursor).toEqual(new BucketCursor('', 7, 1));
|
2019-08-21 15:07:49 +01:00
|
|
|
});
|
|
|
|
});
|