Files
api/web/__test__/store/purchase.test.ts
T
Eli Bosley af5ca11860 Feat/vue (#1655)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- New Features
- Introduced Docker management UI components: Overview, Logs, Console,
Preview, and Edit.
- Added responsive Card/Detail layouts with grouping, bulk actions, and
tabs.
  - New UnraidToaster component and global toaster configuration.
- Component auto-mounting improved with async loading and multi-selector
support.
- UI/UX
- Overhauled theme system (light/dark tokens, primary/orange accents)
and added theme variants.
  - Header OS version now includes integrated changelog modal.
- Registration displays warning states; multiple visual polish updates.
- API
  - CPU load now includes percentGuest and percentSteal metrics.
- Chores
  - Migrated web app to Vite; updated artifacts and manifests.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: mdatelle <mike@datelle.net>
Co-authored-by: Michael Datelle <mdatelle@icloud.com>
2025-09-08 10:04:49 -04:00

214 lines
4.5 KiB
TypeScript

/**
* Purchase store test coverage
*/
import { ref } from 'vue';
import { createPinia, setActivePinia } from 'pinia';
import { PURCHASE_CALLBACK } from '~/helpers/urls';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { usePurchaseStore } from '~/store/purchase';
// Mock dependencies
const mockSend = vi.fn();
const mockServerStore = {
serverPurchasePayload: {
guid: 'test-guid',
name: 'test-server',
},
inIframe: false,
};
// Mock Apollo Composable
vi.mock('@vue/apollo-composable', () => ({
useQuery: () => ({
result: ref({}),
loading: ref(false),
}),
provideApolloClient: vi.fn(),
}));
// Mock Apollo client
vi.mock('~/helpers/create-apollo-client', () => ({
client: {
clearStore: vi.fn().mockResolvedValue(undefined),
stop: vi.fn(),
},
}));
// Mock shared callbacks
vi.mock('@unraid/shared-callbacks', () => {
return {
useCallback: vi.fn(() => ({
send: mockSend,
watcher: vi.fn(),
parse: vi.fn(),
})),
};
});
// Mock activation code data store
vi.mock('~/components/Activation/store/activationCodeData', () => ({
useActivationCodeDataStore: () => ({
activationCode: ref(null),
}),
}));
vi.mock('~/store/callbackActions', () => ({
useCallbackActionsStore: () => ({
send: mockSend,
sendType: 'post',
}),
}));
vi.mock('~/store/server', () => ({
useServerStore: () => mockServerStore,
}));
describe('Purchase Store', () => {
let store: ReturnType<typeof usePurchaseStore>;
beforeEach(() => {
// Reset mock values
mockServerStore.inIframe = false;
setActivePinia(createPinia());
store = usePurchaseStore();
vi.clearAllMocks();
});
afterEach(() => {
vi.resetAllMocks();
});
describe('Actions', () => {
it('should call activate action correctly', () => {
store.activate();
expect(mockSend).toHaveBeenCalledTimes(1);
expect(mockSend).toHaveBeenCalledWith(
PURCHASE_CALLBACK.toString(),
[
{
server: {
guid: 'test-guid',
name: 'test-server',
activationCodeData: null,
},
type: 'activate',
},
],
undefined,
'post'
);
});
it('should call redeem action correctly', () => {
store.redeem();
expect(mockSend).toHaveBeenCalledTimes(1);
expect(mockSend).toHaveBeenCalledWith(
PURCHASE_CALLBACK.toString(),
[
{
server: {
guid: 'test-guid',
name: 'test-server',
},
type: 'redeem',
},
],
undefined,
'post'
);
});
it('should call purchase action correctly', () => {
store.purchase();
expect(mockSend).toHaveBeenCalledTimes(1);
expect(mockSend).toHaveBeenCalledWith(
PURCHASE_CALLBACK.toString(),
[
{
server: {
guid: 'test-guid',
name: 'test-server',
},
type: 'purchase',
},
],
undefined,
'post'
);
});
it('should call upgrade action correctly', () => {
store.upgrade();
expect(mockSend).toHaveBeenCalledTimes(1);
expect(mockSend).toHaveBeenCalledWith(
PURCHASE_CALLBACK.toString(),
[
{
server: {
guid: 'test-guid',
name: 'test-server',
},
type: 'upgrade',
},
],
undefined,
'post'
);
});
it('should call renew action correctly', () => {
store.renew();
expect(mockSend).toHaveBeenCalledTimes(1);
expect(mockSend).toHaveBeenCalledWith(
PURCHASE_CALLBACK.toString(),
[
{
server: {
guid: 'test-guid',
name: 'test-server',
},
type: 'renew',
},
],
undefined,
'post'
);
});
it('should handle iframe redirection correctly', () => {
// Set up the iframe state
mockServerStore.inIframe = true;
setActivePinia(createPinia());
const iframeStore = usePurchaseStore();
iframeStore.purchase();
expect(mockSend).toHaveBeenCalledTimes(1);
expect(mockSend).toHaveBeenCalledWith(
PURCHASE_CALLBACK.toString(),
[
{
server: {
guid: 'test-guid',
name: 'test-server',
},
type: 'purchase',
},
],
'newTab',
'post'
);
});
});
});