koel/resources/assets/js/services/authService.spec.ts

32 lines
1 KiB
TypeScript
Raw Normal View History

2022-05-14 18:49:45 +00:00
import UnitTestCase from '@/__tests__/UnitTestCase'
import { localStorageService } from '@/services/localStorageService'
import { expect, it } from 'vitest'
2022-09-14 16:45:08 +00:00
import { authService } from './authService'
2022-05-14 18:49:45 +00:00
new class extends UnitTestCase {
protected test () {
it('gets the token', () => {
const mock = this.mock(localStorageService, 'get')
2022-11-16 17:57:38 +00:00
authService.getApiToken()
2022-05-14 18:49:45 +00:00
expect(mock).toHaveBeenCalledWith('api-token')
})
it.each([['foo', true], [null, false]])('checks if the token exists', (token, exists) => {
this.mock(localStorageService, 'get', token)
2022-11-16 17:57:38 +00:00
expect(authService.hasApiToken()).toBe(exists)
2022-05-14 18:49:45 +00:00
})
it('sets the token', () => {
const mock = this.mock(localStorageService, 'set')
2022-11-16 17:57:38 +00:00
authService.setApiToken('foo')
2022-05-14 18:49:45 +00:00
expect(mock).toHaveBeenCalledWith('api-token', 'foo')
})
it('destroys the token', () => {
const mock = this.mock(localStorageService, 'remove')
authService.destroy()
expect(mock).toHaveBeenCalledWith('api-token')
})
}
}