|
| 1 | +import { getters } from '../getters' |
| 2 | +import { mutations } from '../mutations' |
| 3 | +import { defaultState } from '../state' |
| 4 | + |
| 5 | +const mockServerFilesGet = vi.fn() |
| 6 | + |
| 7 | +vi.mock('@/api/httpClientActions', () => ({ |
| 8 | + httpClientActions: { |
| 9 | + serverFilesGet: (...args: any[]) => mockServerFilesGet(...args) |
| 10 | + } |
| 11 | +})) |
| 12 | + |
| 13 | +import { actions } from '../actions' |
| 14 | + |
| 15 | +describe('plugins store', () => { |
| 16 | + beforeEach(() => { |
| 17 | + vi.clearAllMocks() |
| 18 | + }) |
| 19 | + |
| 20 | + describe('state', () => { |
| 21 | + it('returns default state', () => { |
| 22 | + const state = defaultState() |
| 23 | + expect(state.naviPoints).toEqual([]) |
| 24 | + expect(state.naviPointsLoaded).toBe(false) |
| 25 | + }) |
| 26 | + }) |
| 27 | + |
| 28 | + describe('getters', () => { |
| 29 | + it('getNaviPoints returns naviPoints from state', () => { |
| 30 | + const state = { |
| 31 | + naviPoints: [ |
| 32 | + { title: 'Test', href: '/test', target: '_self', icon: 'mdi-test', position: 1 } |
| 33 | + ], |
| 34 | + naviPointsLoaded: true |
| 35 | + } |
| 36 | + const result = getters.getNaviPoints(state, {} as any, {} as any, {} as any) |
| 37 | + expect(result).toEqual(state.naviPoints) |
| 38 | + }) |
| 39 | + |
| 40 | + it('getNaviPointsLoaded returns loaded status from state', () => { |
| 41 | + const state = { |
| 42 | + naviPoints: [], |
| 43 | + naviPointsLoaded: true |
| 44 | + } |
| 45 | + const result = getters.getNaviPointsLoaded(state, {} as any, {} as any, {} as any) |
| 46 | + expect(result).toBe(true) |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + describe('mutations', () => { |
| 51 | + it('setReset resets state', () => { |
| 52 | + const state = { |
| 53 | + naviPoints: [{ title: 'Test', href: '/test', target: '_self', icon: '', position: 1 }], |
| 54 | + naviPointsLoaded: true |
| 55 | + } |
| 56 | + mutations.setReset(state) |
| 57 | + expect(state.naviPoints).toEqual([]) |
| 58 | + expect(state.naviPointsLoaded).toBe(false) |
| 59 | + }) |
| 60 | + |
| 61 | + it('setNaviPoints sets naviPoints', () => { |
| 62 | + const state = defaultState() |
| 63 | + const points = [ |
| 64 | + { title: 'KlipperFleet', href: '/klipperfleet.html', target: '_self', icon: 'mdi-fleet', position: 86 } |
| 65 | + ] |
| 66 | + mutations.setNaviPoints(state, points) |
| 67 | + expect(state.naviPoints).toEqual(points) |
| 68 | + }) |
| 69 | + |
| 70 | + it('setNaviPointsLoaded sets loaded status', () => { |
| 71 | + const state = defaultState() |
| 72 | + mutations.setNaviPointsLoaded(state, true) |
| 73 | + expect(state.naviPointsLoaded).toBe(true) |
| 74 | + }) |
| 75 | + }) |
| 76 | + |
| 77 | + describe('actions', () => { |
| 78 | + it('reset dispatches setReset mutation', async () => { |
| 79 | + const commit = vi.fn() |
| 80 | + await (actions as any).reset({ commit }) |
| 81 | + expect(commit).toHaveBeenCalledWith('setReset') |
| 82 | + }) |
| 83 | + |
| 84 | + it('fetchNaviPoints fetches and sets navi points', async () => { |
| 85 | + const mockPoints = [ |
| 86 | + { title: 'KlipperFleet', href: '/klipperfleet.html', target: '_self', icon: '', position: 86 } |
| 87 | + ] |
| 88 | + |
| 89 | + mockServerFilesGet.mockResolvedValue({ |
| 90 | + data: mockPoints |
| 91 | + }) |
| 92 | + |
| 93 | + const commit = vi.fn() |
| 94 | + const rootState = { config: { apiUrl: 'http://localhost' } } |
| 95 | + |
| 96 | + await (actions as any).fetchNaviPoints({ commit, rootState } as any) |
| 97 | + |
| 98 | + expect(commit).toHaveBeenCalledWith('setNaviPoints', expect.any(Array)) |
| 99 | + expect(commit).toHaveBeenCalledWith('setNaviPointsLoaded', true) |
| 100 | + }) |
| 101 | + |
| 102 | + it('fetchNaviPoints handles errors gracefully', async () => { |
| 103 | + mockServerFilesGet.mockRejectedValue(new Error('Not found')) |
| 104 | + |
| 105 | + const commit = vi.fn() |
| 106 | + const rootState = { config: { apiUrl: 'http://localhost' } } |
| 107 | + |
| 108 | + await (actions as any).fetchNaviPoints({ commit, rootState } as any) |
| 109 | + |
| 110 | + expect(commit).toHaveBeenCalledWith('setNaviPointsLoaded', true) |
| 111 | + expect(commit).not.toHaveBeenCalledWith('setNaviPoints', expect.any(Array)) |
| 112 | + }) |
| 113 | + }) |
| 114 | +}) |
0 commit comments