Files
Rogeeandrogee f50eacdc92 H-289: add Captain Skill management UI (#47)
* H-289: add Captain Skill management UI

* H-289: guard Captain Skill assistant switches

---------

Co-authored-by: Rogee <rogee@ipao.vip>
2026-08-18 15:34:26 +08:00

94 lines
2.4 KiB
JavaScript

import CaptainSkillsAPI from 'dashboard/api/captain/skills';
import store from './skills';
vi.mock('dashboard/api/captain/skills', () => ({
default: {
get: vi.fn(),
bind: vi.fn(),
unbind: vi.fn(),
},
}));
describe('captainSkills store', () => {
beforeEach(() => vi.clearAllMocks());
it('keeps only the latest assistant list response and loading state', async () => {
let resolveA;
let resolveB;
CaptainSkillsAPI.get
.mockReturnValueOnce(
new Promise(resolve => {
resolveA = resolve;
})
)
.mockReturnValueOnce(
new Promise(resolve => {
resolveB = resolve;
})
);
const commit = vi.fn();
const requestA = store.actions.get({ commit }, { assistantId: 1 });
const requestB = store.actions.get({ commit }, { assistantId: 2 });
resolveB({ data: { payload: ['B'], meta: { page: 2 } } });
await requestB;
resolveA({ data: { payload: ['A'], meta: { page: 1 } } });
await requestA;
expect(commit).toHaveBeenCalledWith('SET_CAPTAINSKILL', ['B']);
expect(commit).toHaveBeenCalledWith('SET_CAPTAINSKILL_META', {
page: 2,
});
expect(commit).not.toHaveBeenCalledWith('SET_CAPTAINSKILL', ['A']);
expect(commit).not.toHaveBeenCalledWith('SET_CAPTAINSKILL_META', {
page: 1,
});
expect(
commit.mock.calls.filter(
([type, value]) =>
type === 'SET_CAPTAINSKILL_UI_FLAG' &&
value.fetchingList === false
)
).toHaveLength(1);
});
it.each(['bind', 'unbind'])(
'%s toggles the row loading state',
async action => {
CaptainSkillsAPI[action].mockResolvedValue({});
const commit = vi.fn();
const payload = { assistantId: 2, skillId: 3 };
await store.actions[action]({ commit }, payload);
expect(CaptainSkillsAPI[action]).toHaveBeenCalledWith(payload);
expect(commit).toHaveBeenNthCalledWith(
1,
'SET_CAPTAINSKILL_UI_FLAG',
{
updatingItem: true,
}
);
expect(commit).toHaveBeenLastCalledWith(
'SET_CAPTAINSKILL_UI_FLAG',
{
updatingItem: false,
}
);
}
);
it('restores loading state when binding fails', async () => {
const error = new Error('conflict');
CaptainSkillsAPI.bind.mockRejectedValue(error);
const commit = vi.fn();
await expect(
store.actions.bind({ commit }, { assistantId: 2, skillId: 3 })
).rejects.toBe(error);
expect(commit).toHaveBeenLastCalledWith('SET_CAPTAINSKILL_UI_FLAG', {
updatingItem: false,
});
});
});