-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testing(store): add basic store testing
- Loading branch information
1 parent
5e257e0
commit c816307
Showing
12 changed files
with
1,419 additions
and
619 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
import { beforeEach, describe, expect, it } from 'vitest'; | ||
import { createPinia, setActivePinia } from 'pinia'; | ||
|
||
import { useEntityStore } from '../../app/stores/entityStore'; | ||
|
||
describe('entityStore', () => { | ||
beforeEach(() => { | ||
setActivePinia(createPinia()); | ||
}); | ||
|
||
it('should be defined', () => { | ||
const store = useEntityStore('test'); | ||
expect(store).toBeDefined(); | ||
}); | ||
|
||
it('should have a default state', () => { | ||
const store = useEntityStore('test'); | ||
expect(store.getState()).toEqual({ entities: {}, ids: [] }); | ||
}); | ||
|
||
describe('upsertMany', () => { | ||
it('should insert many entities', () => { | ||
const store = useEntityStore('test'); | ||
store.upsertMany([{ id: '1' }, { id: '2' }]); | ||
|
||
expect(store.getState()).toEqual({ | ||
entities: { '1': { id: '1' }, '2': { id: '2' } }, | ||
ids: ['1', '2'], | ||
}); | ||
}); | ||
|
||
it('should update existing entities', () => { | ||
const store = useEntityStore<{ id: string, name: string }>('test'); | ||
store.insert({ id: '1', name: 'empty' }); | ||
store.upsertMany([{ id: '1', name: 'test' }]); | ||
|
||
expect(store.getState()).toEqual({ | ||
entities: { '1': { id: '1', name: 'test' } }, | ||
ids: ['1'], | ||
}); | ||
}); | ||
|
||
it('should not insert entities without an id', () => { | ||
const store = useEntityStore('test'); | ||
// @ts-expect-error: id is missing | ||
store.upsertMany([{}, {}]); | ||
|
||
expect(store.getState()).toEqual({ entities: {}, ids: [] }); | ||
}); | ||
}); | ||
|
||
describe('insert', () => { | ||
it('should insert an entity', () => { | ||
const store = useEntityStore('test'); | ||
store.insert({ id: '1' }); | ||
|
||
expect(store.getState()).toEqual({ | ||
entities: { '1': { id: '1' } }, | ||
ids: ['1'], | ||
}); | ||
}); | ||
|
||
|
||
it('should not insert an entity without an id', () => { | ||
const store = useEntityStore('test'); | ||
// @ts-expect-error: id is missing | ||
store.insert({}); | ||
|
||
expect(store.getState()).toEqual({ entities: {}, ids: [] }); | ||
}); | ||
}); | ||
|
||
describe('update', () => { | ||
it('should update an entity', () => { | ||
const store = useEntityStore<{ id: string, name: string }>('test'); | ||
store.insert({ id: '1', name: 'test' }); | ||
store.update('1', { name: 'updated' }); | ||
|
||
expect(store.getState()).toEqual({ | ||
entities: { '1': { id: '1', name: 'updated' } }, | ||
ids: ['1'], | ||
}); | ||
}); | ||
|
||
|
||
it('should not update an entity that does not exist', () => { | ||
const store = useEntityStore('test'); | ||
store.update('1', {}); | ||
|
||
expect(store.getState()).toEqual({ entities: {}, ids: [] }); | ||
}); | ||
}); | ||
|
||
describe('remove', () => { | ||
it('should remove an entity', () => { | ||
const store = useEntityStore('test'); | ||
store.insert({ id: '1' }); | ||
store.remove('1'); | ||
|
||
expect(store.getState()).toEqual({ entities: {}, ids: [] }); | ||
}); | ||
|
||
it('should not remove an entity that does not exist', () => { | ||
const store = useEntityStore('test'); | ||
store.remove('1'); | ||
|
||
expect(store.getState()).toEqual({ entities: {}, ids: [] }); | ||
}); | ||
}); | ||
|
||
describe('getById', () => { | ||
it('should get an entity by id', () => { | ||
const store = useEntityStore<{ id: string, name: string }>('test'); | ||
store.insert({ id: '1', name: 'test' }); | ||
|
||
expect(store.getById('1').value).toEqual({ id: '1', name: 'test' }); | ||
}); | ||
|
||
it('should return undefined if the entity does not exist', () => { | ||
const store = useEntityStore('test'); | ||
|
||
expect(store.getById('1').value).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('getAll', () => { | ||
it('should get entities by ids', () => { | ||
const store = useEntityStore<{ id: string, name: string }>('test'); | ||
store.insert({ id: '1', name: 'test' }); | ||
store.insert({ id: '2', name: 'test' }); | ||
|
||
expect(store.getAll().value).toEqual([ | ||
{ id: '1', name: 'test' }, | ||
{ id: '2', name: 'test' }, | ||
]); | ||
}); | ||
|
||
it('should return an empty array if there are no entities', () => { | ||
const store = useEntityStore('test'); | ||
|
||
expect(store.getAll().value).toEqual([]); | ||
}); | ||
}); | ||
|
||
describe('find', () => { | ||
it('should find entities by predicate', () => { | ||
const store = useEntityStore<{ id: string, name: string }>('test'); | ||
store.insert({ id: '1', name: 'test 1' }); | ||
store.insert({ id: '2', name: 'test 2' }); | ||
|
||
expect(store.find((entity) => entity.name === 'test 1').value).toEqual([ | ||
{ id: '1', name: 'test 1' }, | ||
]); | ||
}); | ||
|
||
it('should return an empty array if there are no entities', () => { | ||
const store = useEntityStore('test'); | ||
|
||
expect(store.find(() => true).value).toEqual([]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import { createPinia, setActivePinia } from 'pinia'; | ||
import { mockNuxtImport } from '@nuxt/test-utils/runtime'; | ||
|
||
import { useSettingsStore } from '../../app/stores/settingsStore'; | ||
|
||
const { useColorMode } = vi.hoisted(() => ({ | ||
useColorMode: () => ({ | ||
value: 'dark', | ||
preference: 'system', | ||
}), | ||
})); | ||
mockNuxtImport('useColorMode', () => useColorMode); | ||
|
||
describe("useSettingsStore", () => { | ||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
setActivePinia(createPinia()); | ||
}); | ||
|
||
it('should have a default state', () => { | ||
const store = useSettingsStore(); | ||
expect(store.settings).toStrictEqual({ language: 'en', themeMode: 'dark', themePrimary: 'sky' }); | ||
}); | ||
|
||
describe('localStorage', () => { | ||
it('should load settings from localStorage', () => { | ||
vi.spyOn(localStorage, 'getItem').mockReturnValue((JSON.stringify({ language: 'fr', themeMode: 'dark', themePrimary: 'red' }))); | ||
|
||
const store = useSettingsStore(); | ||
expect(store.settings).toEqual({ language: 'fr', themeMode: 'dark', themePrimary: 'red' }); | ||
}); | ||
|
||
it('should save settings to localStorage', async () => { | ||
const spy = vi.spyOn(localStorage, 'setItem'); | ||
const store = useSettingsStore(); | ||
store.updateSettings({ language: 'fr' }); | ||
|
||
await nextTick(); | ||
|
||
expect(spy).toHaveBeenCalledWith('settings', JSON.stringify({ language: 'fr', themeMode: 'dark', themePrimary: 'sky' })); | ||
}); | ||
}); | ||
|
||
describe('updateSettings', () => { | ||
it('should update settings', () => { | ||
const store = useSettingsStore(); | ||
store.updateSettings({ language: 'fr' }); | ||
|
||
expect(store.settings).toStrictEqual({ language: 'fr', themeMode: 'dark', themePrimary: 'sky' }); | ||
}); | ||
}); | ||
|
||
describe('setLanguage', () => { | ||
it('should set the language', () => { | ||
const store = useSettingsStore(); | ||
store.setLanguage('fr'); | ||
|
||
expect(store.settings.language).toStrictEqual('fr'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.