Files
api/web/__test__/store/activationCode.test.ts
Michael Datelle d74d9f1246 test: create tests for stores batch 2 (#1351)
This is batch 2 of the web store tests. I started implementing the
recommended approach in the Pinia docs for the store tests and was able
to eliminate most of the mocking files. The server.test.ts file still
uses `pinia/testing` for now since I was having trouble with some of the
dependencies in the store due to it's complexity.

I also updated the `web-testing-rules`for Cursor in an effort to
streamline the AI's approach when helping with tests. There's some
things it still struggles with and seems like it doesn't always take the
rules into consideration until after it hits a snag. It likes to try and
create a mock for the store it's actually testing even though there's a
rule in place and has to be reminded.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

## Summary by CodeRabbit

- **Tests**
- Added comprehensive test suites for account management and activation
flows to ensure smoother user interactions.
- Introduced new test coverage for callback actions, validating state
management and action handling.
- Added a new test file for the account store, covering various actions
and their interactions.
- Introduced a new test file for the activation code store, verifying
state management and modal visibility.
- Established a new test file for dropdown functionality, ensuring
accurate state management and action methods.
- Added a new test suite for the Errors store, covering error handling
and modal interactions.
- Enhanced test guidelines for Vue components and Pinia stores,
providing clearer documentation and best practices.
- Introduced a new test file for the InstallKey store, validating key
installation processes and error handling.
- Added a new test file for the dropdown store, ensuring accurate
visibility state management and action methods.

- **Refactor**
- Streamlined the structure of account action payloads for consistent
behavior.
- Improved code formatting and reactivity setups to enhance overall
operational stability.
- Enhanced reactivity capabilities in various stores by introducing new
Vue composition API functions.
- Improved code readability and organization in the installKey store and
other related files.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: mdatelle <mike@datelle.net>
2025-04-14 12:30:27 -04:00

135 lines
3.6 KiB
TypeScript

/**
* Activation code store test coverage
*/
import { nextTick, ref } from 'vue';
import { createPinia, setActivePinia } from 'pinia';
import { ACTIVATION_CODE_MODAL_HIDDEN_STORAGE_KEY } from '~/consts';
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
import { useActivationCodeStore } from '~/store/activationCode';
// Mock console methods to suppress output
const originalConsoleDebug = console.debug;
const originalConsoleError = console.error;
beforeAll(() => {
console.debug = vi.fn();
console.error = vi.fn();
});
afterAll(() => {
console.debug = originalConsoleDebug;
console.error = originalConsoleError;
});
// Mock sessionStorage
const mockStorage = new Map<string, string>();
vi.stubGlobal('sessionStorage', {
getItem: (key: string) => mockStorage.get(key) ?? null,
setItem: (key: string, value: string) => mockStorage.set(key, value),
removeItem: (key: string) => mockStorage.delete(key),
clear: () => mockStorage.clear(),
});
// Mock dependencies
vi.mock('pinia', async () => {
const mod = await vi.importActual<typeof import('pinia')>('pinia');
return {
...mod,
storeToRefs: () => ({
state: ref('ENOKEYFILE'),
callbackData: ref(null),
}),
};
});
vi.mock('~/store/server', () => ({
useServerStore: () => ({
state: 'ENOKEYFILE',
}),
}));
vi.mock('~/store/callbackActions', () => ({
useCallbackActionsStore: () => ({
callbackData: null,
}),
}));
describe('Activation Code Store', () => {
let store: ReturnType<typeof useActivationCodeStore>;
beforeEach(() => {
setActivePinia(createPinia());
store = useActivationCodeStore();
vi.clearAllMocks();
mockStorage.clear();
});
describe('State and Actions', () => {
const mockData = {
code: 'TEST123',
partnerName: 'Test Partner',
partnerUrl: 'https://test.com',
partnerLogo: true,
};
it('should initialize with null data', () => {
expect(store.code).toBeNull();
expect(store.partnerName).toBeNull();
expect(store.partnerUrl).toBeNull();
expect(store.partnerLogo).toBeNull();
});
it('should set data correctly', () => {
store.setData(mockData);
expect(store.code).toBe('TEST123');
expect(store.partnerName).toBe('Test Partner');
expect(store.partnerUrl).toBe('https://test.com');
expect(store.partnerLogo).toBe('/webGui/images/partner-logo.svg');
});
it('should handle data without optional fields', () => {
store.setData({ code: 'TEST123' });
expect(store.code).toBe('TEST123');
expect(store.partnerName).toBeNull();
expect(store.partnerUrl).toBeNull();
expect(store.partnerLogo).toBeNull();
});
});
describe('Modal Visibility', () => {
it('should show activation modal by default when conditions are met', () => {
store.setData({ code: 'TEST123' });
expect(store.showActivationModal).toBe(true);
});
it('should not show modal when data is null', () => {
expect(store.showActivationModal).toBe(false);
});
it('should handle modal visibility state in session storage', async () => {
store.setData({ code: 'TEST123' });
expect(store.showActivationModal).toBe(true);
store.setActivationModalHidden(true);
await nextTick();
expect(store.showActivationModal).toBe(false);
expect(sessionStorage.getItem(ACTIVATION_CODE_MODAL_HIDDEN_STORAGE_KEY)).toBe('true');
store.setActivationModalHidden(false);
await nextTick();
expect(store.showActivationModal).toBe(true);
expect(sessionStorage.getItem(ACTIVATION_CODE_MODAL_HIDDEN_STORAGE_KEY)).toBeNull();
});
});
});