Whalebird-desktop-client-ma.../spec/renderer/integration/store/Preferences/Account.spec.ts

158 lines
4.4 KiB
TypeScript
Raw Normal View History

2022-04-25 15:33:49 +02:00
import { createStore, Store } from 'vuex'
import { ipcMain, ipcRenderer } from '~/spec/mock/electron'
import Account, { AccountState } from '@/store/Preferences/Account'
import { LocalAccount } from '~/src/types/localAccount'
import { MyWindow } from '~/src/types/global'
2022-04-25 15:33:49 +02:00
import { RootState } from '@/store'
;(window as any as MyWindow).ipcRenderer = ipcRenderer
const account: LocalAccount = {
_id: 'sample',
baseURL: 'http://example.com',
domain: 'example.com',
clientId: 'hoge',
clientSecret: 'hogehoge',
accessToken: null,
refreshToken: null,
username: null,
accountId: null,
avatar: null,
order: 1
}
const state = (): AccountState => {
return {
accounts: [],
accountLoading: false
}
}
const initStore = () => {
return {
namespaced: true,
state: state(),
actions: Account.actions,
mutations: Account.mutations
}
}
2022-04-25 15:33:49 +02:00
const preferencesStore = () => ({
namespaced: true,
modules: {
Account: initStore()
}
})
describe('Account', () => {
2022-04-25 15:33:49 +02:00
let store: Store<RootState>
beforeEach(() => {
2022-04-25 15:33:49 +02:00
store = createStore({
modules: {
2022-04-25 15:33:49 +02:00
Preferences: preferencesStore()
}
})
})
describe('loadAccounts', () => {
it('error', async () => {
2020-11-30 14:30:06 +01:00
ipcMain.handle('list-accounts', async () => {
throw new Error()
})
2022-04-25 15:33:49 +02:00
await store.dispatch('Preferences/Account/loadAccounts').catch((err: Error) => {
expect(err instanceof Error).toEqual(true)
})
2020-11-30 14:30:06 +01:00
ipcMain.removeHandler('list-accounts')
})
it('success', async () => {
2020-11-30 14:30:06 +01:00
ipcMain.handle('list-accounts', () => {
return [account]
})
2022-04-25 15:33:49 +02:00
await store.dispatch('Preferences/Account/loadAccounts')
expect(store.state.Preferences.Account.accounts).toEqual([account])
2020-11-30 14:30:06 +01:00
ipcMain.removeHandler('list-accounts')
})
})
describe('removeAccount', () => {
it('error', async () => {
2020-11-30 14:30:06 +01:00
ipcMain.handle('remove-account', async () => {
throw new Error()
})
2022-04-25 15:33:49 +02:00
await store.dispatch('Preferences/Account/removeAccount', account).catch((err: Error) => {
expect(err instanceof Error).toEqual(true)
})
2020-11-30 14:30:06 +01:00
ipcMain.removeHandler('remove-account')
})
it('success', async () => {
2020-11-30 14:30:06 +01:00
ipcMain.handle('remove-account', () => {
return true
})
2022-04-25 15:33:49 +02:00
const res = await store.dispatch('Preferences/Account/removeAccount', account)
expect(res).toEqual(undefined)
2020-11-30 14:30:06 +01:00
ipcMain.removeHandler('remove-account')
})
})
describe('forwardAccount', () => {
it('error', async () => {
2020-11-30 14:30:06 +01:00
ipcMain.handle('forward-account', async () => {
throw new Error()
})
2022-04-25 15:33:49 +02:00
await store.dispatch('Preferences/Account/forwardAccount', account).catch((err: Error) => {
expect(err instanceof Error).toEqual(true)
})
2020-11-30 14:30:06 +01:00
ipcMain.removeHandler('forward-account')
})
it('success', async () => {
2020-11-30 14:30:06 +01:00
ipcMain.handle('forward-account', () => {
return {}
})
2022-04-25 15:33:49 +02:00
const res = await store.dispatch('Preferences/Account/forwardAccount', account)
expect(res).toEqual(undefined)
2020-11-30 14:30:06 +01:00
ipcMain.removeHandler('forward-account')
})
})
describe('backwardAccount', () => {
it('error', async () => {
2020-11-30 14:30:06 +01:00
ipcMain.handle('backward-account', () => {
throw new Error()
})
2022-04-25 15:33:49 +02:00
await store.dispatch('Preferences/Account/backwardAccount', account).catch((err: Error) => {
expect(err instanceof Error).toEqual(true)
})
2020-11-30 14:30:06 +01:00
ipcMain.removeHandler('backward-account')
})
it('success', async () => {
2020-11-30 14:30:06 +01:00
ipcMain.handle('backward-account', () => {
return {}
})
2022-04-25 15:33:49 +02:00
const res = await store.dispatch('Preferences/Account/backwardAccount', account)
expect(res).toEqual(undefined)
2020-11-30 14:30:06 +01:00
ipcMain.removeHandler('backward-account')
})
})
describe('removeAllAccounts', () => {
it('error', async () => {
2020-11-30 14:30:06 +01:00
ipcMain.handle('remove-all-accounts', () => {
throw new Error()
})
2022-04-25 15:33:49 +02:00
await store.dispatch('Preferences/Account/removeAllAccounts', account).catch((err: Error) => {
expect(err instanceof Error).toEqual(true)
})
2020-11-30 14:30:06 +01:00
ipcMain.removeHandler('remove-all-accounts')
})
it('success', async () => {
2020-11-30 14:30:06 +01:00
ipcMain.handle('remove-all-accounts', () => {
return {}
})
2022-04-25 15:33:49 +02:00
const res = await store.dispatch('Preferences/Account/removeAllAccounts', account)
expect(res).toEqual(undefined)
2020-11-30 14:30:06 +01:00
ipcMain.removeHandler('remove-all-accounts')
})
})
})