mirror of
https://github.com/CzBiX/qb-web.git
synced 2026-11-29 15:25:27 +08:00
Add more unit test
This commit is contained in:
@@ -44,21 +44,7 @@
|
||||
<script lang="ts">
|
||||
import Vue, { PropType } from 'vue';
|
||||
import { mapState, mapMutations } from 'vuex';
|
||||
|
||||
export interface Group {
|
||||
title: string;
|
||||
icon: string;
|
||||
children: Child[];
|
||||
model: boolean;
|
||||
select: string;
|
||||
}
|
||||
|
||||
export interface Child {
|
||||
title: string;
|
||||
key: string | null;
|
||||
icon: string;
|
||||
append: string | null;
|
||||
}
|
||||
import { FilterGroup as types } from '../types';
|
||||
|
||||
interface Data {
|
||||
model: boolean;
|
||||
@@ -67,7 +53,7 @@ interface Data {
|
||||
|
||||
export default Vue.extend({
|
||||
props: {
|
||||
group: Object as PropType<Group>,
|
||||
group: Object as PropType<types.Group>,
|
||||
},
|
||||
data(): Data {
|
||||
return {
|
||||
|
||||
16
src/components/types.ts
Normal file
16
src/components/types.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
export declare module FilterGroup {
|
||||
export interface Group {
|
||||
title: string;
|
||||
icon: string;
|
||||
children: Child[];
|
||||
model: boolean;
|
||||
select: string;
|
||||
}
|
||||
|
||||
export interface Child {
|
||||
title: string;
|
||||
key: string | null;
|
||||
icon: string;
|
||||
append: string | null;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import _ from 'lodash';
|
||||
import Vue from 'vue';
|
||||
import { Module } from 'vuex';
|
||||
import { RootState, ConfigState } from './types';
|
||||
import { ConfigState } from './types';
|
||||
|
||||
const configKey = 'qb-config';
|
||||
|
||||
@@ -18,11 +18,11 @@ const defaultConfig = {
|
||||
};
|
||||
|
||||
function saveConfig(obj: any) {
|
||||
localStorage[configKey] = JSON.stringify(obj);
|
||||
localStorage.setItem(configKey, JSON.stringify(obj));
|
||||
}
|
||||
|
||||
function loadConfig() {
|
||||
const tmp = localStorage[configKey];
|
||||
const tmp = localStorage.getItem(configKey);
|
||||
if (!tmp) {
|
||||
return {};
|
||||
}
|
||||
@@ -30,15 +30,21 @@ function loadConfig() {
|
||||
return JSON.parse(tmp);
|
||||
}
|
||||
|
||||
export const configStore : Module<ConfigState, RootState> = {
|
||||
state: {
|
||||
userConfig: loadConfig(),
|
||||
export const configStore : Module<ConfigState, any> = {
|
||||
state() {
|
||||
return {
|
||||
userConfig: loadConfig(),
|
||||
};
|
||||
},
|
||||
mutations: {
|
||||
updateConfig(state, payload) {
|
||||
const { key, value } = payload;
|
||||
const tmp = _.merge({}, state.userConfig[key], value);
|
||||
Vue.set(state.userConfig, key, tmp);
|
||||
if (_.isPlainObject(value)) {
|
||||
const tmp = _.merge({}, state.userConfig, value);
|
||||
Vue.set(state.userConfig, key, tmp);
|
||||
} else {
|
||||
Vue.set(state.userConfig, key, value);
|
||||
}
|
||||
|
||||
saveConfig(state.userConfig);
|
||||
},
|
||||
|
||||
107
tests/unit/components/FilterGroup.spec.ts
Normal file
107
tests/unit/components/FilterGroup.spec.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
import Vuex from 'vuex';
|
||||
import { createLocalVue, shallowMount } from '@vue/test-utils';
|
||||
import '@/directives';
|
||||
import FilterGroup from '@/components/drawer/FilterGroup.vue';
|
||||
import { FilterGroup as types } from '@/components/types';
|
||||
import { mock } from '../utils';
|
||||
|
||||
const localVue = createLocalVue();
|
||||
localVue.use(Vuex);
|
||||
|
||||
const userConfig = {
|
||||
filter: {
|
||||
foo: 'bar',
|
||||
},
|
||||
};
|
||||
|
||||
const store = new Vuex.Store({
|
||||
getters: {
|
||||
config() {
|
||||
return userConfig;
|
||||
},
|
||||
},
|
||||
mutations: {
|
||||
updateConfig: jest.fn(),
|
||||
},
|
||||
});
|
||||
|
||||
function mount(propsData: object) {
|
||||
return shallowMount(FilterGroup, {
|
||||
localVue,
|
||||
store,
|
||||
propsData,
|
||||
stubs: [
|
||||
'v-list-group',
|
||||
'v-list-item',
|
||||
'v-list-item-icon',
|
||||
'v-list-item-title',
|
||||
'v-list-item-content',
|
||||
'v-img',
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
const emptyGroup: types.Group = {
|
||||
title: '',
|
||||
icon: '',
|
||||
children: [],
|
||||
model: false,
|
||||
select: '',
|
||||
};
|
||||
|
||||
const emptyChild: types.Child = {
|
||||
title: '',
|
||||
key: null,
|
||||
icon: '',
|
||||
append: null,
|
||||
};
|
||||
|
||||
const mockGroup = mock(emptyGroup);
|
||||
const mockChild = mock(emptyChild);
|
||||
|
||||
test('normal create', () => {
|
||||
const group = mockGroup({
|
||||
children: [
|
||||
mockChild({
|
||||
key: 'bar',
|
||||
}),
|
||||
],
|
||||
select: 'foo',
|
||||
model: true,
|
||||
});
|
||||
|
||||
const wrapper = mount({
|
||||
group,
|
||||
});
|
||||
const vmAny = wrapper.vm as any;
|
||||
|
||||
expect(vmAny.selected).toEqual('bar');
|
||||
expect(vmAny.model).toBeTruthy();
|
||||
});
|
||||
|
||||
test('manual select child', () => {
|
||||
const group = mockGroup({
|
||||
select: 'foo',
|
||||
});
|
||||
|
||||
const wrapper = mount({
|
||||
group,
|
||||
});
|
||||
const vmAny = wrapper.vm as any;
|
||||
|
||||
vmAny.select('ha');
|
||||
expect(vmAny.selected).toEqual('ha');
|
||||
});
|
||||
|
||||
test('unselect if can not found children', () => {
|
||||
const group = mockGroup({
|
||||
select: 'foo',
|
||||
});
|
||||
|
||||
const wrapper = mount({
|
||||
group,
|
||||
});
|
||||
const { vm } = wrapper;
|
||||
|
||||
expect((wrapper.vm as any).selected).toBeNull();
|
||||
});
|
||||
82
tests/unit/store/config.spec.ts
Normal file
82
tests/unit/store/config.spec.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import Vuex from 'vuex';
|
||||
import { createLocalVue } from '@vue/test-utils';
|
||||
import { configStore } from '@/store/config';
|
||||
|
||||
|
||||
const localVue = createLocalVue();
|
||||
localVue.use(Vuex);
|
||||
|
||||
const store = new Vuex.Store({
|
||||
modules: {
|
||||
config: configStore,
|
||||
},
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
store.replaceState({
|
||||
config: {
|
||||
userConfig: {},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('load config', () => {
|
||||
const spyGet = jest.spyOn(Object.getPrototypeOf(localStorage), 'getItem');
|
||||
spyGet.mockReturnValue('{"foo": "bar"}');
|
||||
|
||||
// eslint-disable-next-line no-shadow
|
||||
const store = new Vuex.Store({
|
||||
modules: {
|
||||
config: configStore,
|
||||
},
|
||||
});
|
||||
|
||||
expect(store.getters.config).toMatchObject({
|
||||
foo: 'bar',
|
||||
});
|
||||
|
||||
spyGet.mockRestore();
|
||||
});
|
||||
|
||||
test('config getter', () => {
|
||||
expect(store.getters.config).not.toEqual({});
|
||||
});
|
||||
|
||||
describe('update config', () => {
|
||||
const spySet = jest.spyOn(Object.getPrototypeOf(localStorage), 'setItem');
|
||||
|
||||
beforeEach(() => {
|
||||
spySet.mockClear();
|
||||
});
|
||||
afterAll(() => {
|
||||
spySet.mockRestore();
|
||||
});
|
||||
|
||||
test('update object', () => {
|
||||
store.commit('updateConfig', {
|
||||
key: 'obj',
|
||||
value: {
|
||||
foo: 'bar',
|
||||
},
|
||||
});
|
||||
|
||||
expect(store.getters.config).toMatchObject({
|
||||
obj: {
|
||||
foo: 'bar',
|
||||
},
|
||||
});
|
||||
expect(spySet).toBeCalled();
|
||||
});
|
||||
|
||||
test('update plain type', () => {
|
||||
store.commit('updateConfig', {
|
||||
key: 'foo',
|
||||
value: 'bar',
|
||||
});
|
||||
|
||||
expect(store.getters.config).toMatchObject({
|
||||
foo: 'bar',
|
||||
});
|
||||
expect(spySet).toBeCalled();
|
||||
});
|
||||
});
|
||||
62
tests/unit/store/index.spec.ts
Normal file
62
tests/unit/store/index.spec.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import Vuex from 'vuex';
|
||||
import { createLocalVue } from '@vue/test-utils';
|
||||
import store from '@/store';
|
||||
import { RootState } from '@/store/types';
|
||||
import { mock, mockBaseTorrent } from '../utils';
|
||||
|
||||
|
||||
const localVue = createLocalVue();
|
||||
localVue.use(Vuex);
|
||||
|
||||
const emtpyState: RootState = {
|
||||
rid: 0,
|
||||
mainData: undefined,
|
||||
preferences: null,
|
||||
pasteUrl: null,
|
||||
};
|
||||
|
||||
const mockState = mock(emtpyState);
|
||||
|
||||
beforeEach(() => {
|
||||
store.replaceState(emtpyState);
|
||||
});
|
||||
|
||||
test('update preferences', () => {
|
||||
const obj = {
|
||||
url: 'something',
|
||||
};
|
||||
store.commit('updatePreferences', obj);
|
||||
|
||||
expect(store.state.preferences).toEqual(obj);
|
||||
});
|
||||
|
||||
test('set paste url', () => {
|
||||
store.commit('setPasteUrl', {
|
||||
url: 'something',
|
||||
});
|
||||
|
||||
expect(store.state.pasteUrl).toEqual('something');
|
||||
});
|
||||
|
||||
describe('all torrents getter', () => {
|
||||
test('empty', () => {
|
||||
expect(store.getters.allTorrents).toEqual([]);
|
||||
});
|
||||
|
||||
test('with data', () => {
|
||||
store.replaceState(mockState({
|
||||
mainData: {
|
||||
categories: {},
|
||||
server_state: undefined as any,
|
||||
torrents: {
|
||||
a: mockBaseTorrent({}),
|
||||
b: mockBaseTorrent({}),
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
expect(store.getters.allTorrents).toMatchObject([
|
||||
{ hash: 'a' }, { hash: 'b' },
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -1,7 +1,6 @@
|
||||
import { Torrent } from '@/types';
|
||||
import { Torrent, BaseTorrent } from '@/types';
|
||||
|
||||
const emptyTorrent: Torrent = {
|
||||
hash: '',
|
||||
const emptyBaseTorrent: BaseTorrent = {
|
||||
added_on: 0,
|
||||
amount_left: 0,
|
||||
auto_tmm: false,
|
||||
@@ -46,6 +45,13 @@ const emptyTorrent: Torrent = {
|
||||
upspeed: 0,
|
||||
};
|
||||
|
||||
export function mockTorrent(props: Partial<Torrent>): Torrent {
|
||||
return Object.assign({}, emptyTorrent, props);
|
||||
const emptyTorrent: Torrent = Object.assign({}, emptyBaseTorrent, {
|
||||
hash: '',
|
||||
});
|
||||
|
||||
export function mock<T>(empty: T) {
|
||||
return (props: Partial<T>): T => Object.assign({}, empty, props);
|
||||
}
|
||||
|
||||
export const mockBaseTorrent = mock(emptyBaseTorrent);
|
||||
export const mockTorrent = mock(emptyTorrent);
|
||||
|
||||
Reference in New Issue
Block a user