2022-05-15 14:57:28 +00:00
|
|
|
import { expect, it } from 'vitest'
|
|
|
|
import factory from '@/__tests__/factory'
|
2024-01-29 21:58:50 +00:00
|
|
|
import { http } from '@/services'
|
2022-05-15 14:57:28 +00:00
|
|
|
import UnitTestCase from '@/__tests__/UnitTestCase'
|
2024-01-29 21:58:50 +00:00
|
|
|
import { defaultPreferences, preferenceStore } from '.'
|
2022-05-15 14:57:28 +00:00
|
|
|
|
|
|
|
new class extends UnitTestCase {
|
|
|
|
protected beforeEach () {
|
2024-01-29 21:58:50 +00:00
|
|
|
super.beforeEach(() => preferenceStore.init())
|
2022-05-15 14:57:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected test () {
|
2024-01-29 21:58:50 +00:00
|
|
|
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)
|
2024-01-29 21:58:50 +00:00
|
|
|
expect(mock).toHaveBeenCalledWith('me/preferences', { key: 'volume', value: 5 })
|
2022-05-15 14:57:28 +00:00
|
|
|
|
|
|
|
// test the proxy
|
|
|
|
preferenceStore.volume = 6
|
2024-01-29 21:58:50 +00:00
|
|
|
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', () => {
|
2024-01-29 21:58:50 +00:00
|
|
|
const mock = this.mock(http, 'patch')
|
2022-05-15 14:57:28 +00:00
|
|
|
preferenceStore.set('volume', 4.2)
|
2024-01-29 21:58:50 +00:00
|
|
|
expect(mock).toHaveBeenCalledWith('me/preferences', { key: 'volume', value: 4.2 })
|
2022-05-15 14:57:28 +00:00
|
|
|
|
2024-01-29 21:58:50 +00:00
|
|
|
expect(preferenceStore.get('volume')).toBe(4.2)
|
2022-05-15 14:57:28 +00:00
|
|
|
expect(preferenceStore.volume).toBe(4.2)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|