Merge pull request #864 from h3poteto/iss-850

refs #850 Replace Login with typescript
This commit is contained in:
AkiraFukushima 2019-04-04 22:28:40 +09:00 committed by GitHub
commit 2942fe11c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 59 deletions

View File

@ -2,13 +2,12 @@ import { createLocalVue } from '@vue/test-utils'
import Vuex from 'vuex'
import { ipcMain } from '~/spec/mock/electron'
import axios from 'axios'
import Login from '@/store/Login'
import Login, { LoginState } from '@/store/Login'
jest.mock('axios')
const state = () => {
const state = (): LoginState => {
return {
instances: [],
selectedInstance: null,
searching: false
}
@ -79,8 +78,8 @@ describe('Login', () => {
})
})
mockedAxios.get.mockImplementation(() => res)
const data = await store.dispatch('Login/confirmInstance', 'pleroma.io')
expect(data).toEqual('test')
const result = await store.dispatch('Login/confirmInstance', 'pleroma.io')
expect(result).toEqual(true)
expect(store.state.Login.selectedInstance).toEqual('pleroma.io')
})
})

View File

@ -1,24 +1,23 @@
import Login from '@/store/Login'
import Login, { LoginState, MUTATION_TYPES } from '@/store/Login'
describe('Login', () => {
describe('mutations', () => {
let state: any
let state: LoginState
beforeEach(() => {
state = {
instances: [],
selectedInstance: null,
searching: false
}
})
describe('changeInstance', () => {
it('should be changed', () => {
Login.mutations.changeInstance(state, 'pleroma.io')
Login.mutations![MUTATION_TYPES.CHANGE_INSTANCE](state, 'pleroma.io')
expect(state.selectedInstance).toEqual('pleroma.io')
})
})
describe('changeSearching', () => {
it('should be changed', () => {
Login.mutations.changeSearching(state, true)
Login.mutations![MUTATION_TYPES.CHANGE_SEARCHING](state, true)
expect(state.searching).toEqual(true)
})
})

View File

@ -106,7 +106,7 @@ const actions: ActionTree<AppState, any> = {
loadPreferences: ({ commit, dispatch }) => {
return new Promise((resolve, reject) => {
ipcRenderer.send('get-preferences')
ipcRenderer.once('error-get-preferences', (_, err) => {
ipcRenderer.once('error-get-preferences', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-get-preferences')
reject(err)
})

View File

@ -1,48 +0,0 @@
import { ipcRenderer } from 'electron'
import axios from 'axios'
const Login = {
namespaced: true,
state: {
instances: [],
selectedInstance: null,
searching: false
},
mutations: {
changeInstance (state, instance) {
state.selectedInstance = instance
},
changeSearching (state, value) {
state.searching = value
}
},
actions: {
fetchLogin (_, instance) {
return new Promise((resolve, reject) => {
ipcRenderer.send('get-auth-url', instance)
ipcRenderer.once('error-get-auth-url', (event, err) => {
ipcRenderer.removeAllListeners('response-get-auth-url')
reject(err)
})
ipcRenderer.once('response-get-auth-url', (event, url) => {
ipcRenderer.removeAllListeners('response-get-auth-url')
resolve(url)
})
})
},
pageBack ({ commit }) {
commit('changeInstance', null)
},
async confirmInstance ({ commit }, domain) {
commit('changeSearching', true)
const res = await axios.get(`https://${domain}/api/v1/instance`)
.finally(() => {
commit('changeSearching', false)
})
commit('changeInstance', domain)
return res.data
}
}
}
export default Login

View File

@ -0,0 +1,66 @@
import { ipcRenderer } from 'electron'
import axios from 'axios'
import { Module, MutationTree, ActionTree } from 'vuex'
export interface LoginState {
selectedInstance: string | null,
searching: boolean
}
const state = (): LoginState => ({
selectedInstance: null,
searching: false
})
export const MUTATION_TYPES = {
CHANGE_INSTANCE: 'changeInstance',
CHANGE_SEARCHING: 'changeSearching'
}
const mutations: MutationTree<LoginState> = {
[MUTATION_TYPES.CHANGE_INSTANCE]: (state: LoginState, instance: string) => {
state.selectedInstance = instance
},
[MUTATION_TYPES.CHANGE_SEARCHING]: (state: LoginState, searching: boolean) => {
state.searching = searching
}
}
// TODO: use type of rootState
const actions: ActionTree<LoginState, any> = {
fetchLogin: (_, instance: string) => {
return new Promise((resolve, reject) => {
ipcRenderer.send('get-auth-url', instance)
ipcRenderer.once('error-get-auth-url', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-get-auth-url')
reject(err)
})
ipcRenderer.once('response-get-auth-url', (_, url: string) => {
ipcRenderer.removeAllListeners('response-get-auth-url')
resolve(url)
})
})
},
pageBack: ({ commit }) => {
commit(MUTATION_TYPES.CHANGE_INSTANCE, null)
},
confirmInstance: async ({ commit }, domain: string): Promise<boolean> => {
commit(MUTATION_TYPES.CHANGE_SEARCHING, true)
await axios.get(`https://${domain}/api/v1/instance`)
.finally(() => {
commit(MUTATION_TYPES.CHANGE_SEARCHING, false)
})
commit(MUTATION_TYPES.CHANGE_INSTANCE, domain)
return true
}
}
// TODO: use type of rootState
const Login: Module<LoginState, any> = {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
}
export default Login