Merge pull request #864 from h3poteto/iss-850
refs #850 Replace Login with typescript
This commit is contained in:
commit
2942fe11c4
|
@ -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')
|
||||
})
|
||||
})
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
|
|
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue