koel/resources/assets/js/stores/preferenceStore.spec.ts

41 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-05-15 14:57:28 +00:00
import { expect, it } from 'vitest'
import factory from '@/__tests__/factory'
import { http } from '@/services'
2022-05-15 14:57:28 +00:00
import UnitTestCase from '@/__tests__/UnitTestCase'
import { defaultPreferences, preferenceStore } from '.'
2022-05-15 14:57:28 +00:00
new class extends UnitTestCase {
protected beforeEach () {
super.beforeEach(() => preferenceStore.init())
2022-05-15 14:57:28 +00:00
}
protected test () {
it('sets preferences and saves the state', () => {
const user = factory<User>('user')
user.preferences = defaultPreferences
const mock = this.mock(http, 'patch')
2022-05-15 14:57:28 +00:00
preferenceStore.set('volume', 5)
expect(mock).toHaveBeenCalledWith('me/preferences', { key: 'volume', value: 5 })
2022-05-15 14:57:28 +00:00
// test the proxy
preferenceStore.volume = 6
expect(mock).toHaveBeenCalledWith('me/preferences', { key: 'volume', value: 6 })
})
it('does not trigger a request if the value is the same', () => {
const mock = this.mock(http, 'patch')
preferenceStore.set('volume', preferenceStore.volume)
expect(mock).not.toHaveBeenCalled()
2022-05-15 14:57:28 +00:00
})
it('returns preference values', () => {
const mock = this.mock(http, 'patch')
2022-05-15 14:57:28 +00:00
preferenceStore.set('volume', 4.2)
expect(mock).toHaveBeenCalledWith('me/preferences', { key: 'volume', value: 4.2 })
2022-05-15 14:57:28 +00:00
expect(preferenceStore.get('volume')).toBe(4.2)
2022-05-15 14:57:28 +00:00
expect(preferenceStore.volume).toBe(4.2)
})
}
}