Files
api/web/store/activationCode.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

81 lines
2.6 KiB
TypeScript

import { computed, ref, watch } from 'vue';
import { createPinia, defineStore, setActivePinia, storeToRefs } from 'pinia';
import { ACTIVATION_CODE_MODAL_HIDDEN_STORAGE_KEY } from '~/consts';
import { useCallbackActionsStore } from '~/store/callbackActions';
import { useServerStore } from '~/store/server';
setActivePinia(createPinia()); /** required in web component context */
export interface ActivationCodeData {
background?: string;
caseIcon?: string;
code: string;
comment?: string;
header?: string;
headermetacolor?: string;
partnerLogo?: boolean;
partnerName?: string;
partnerUrl?: string;
showBannerGradient?: 'yes';
sysModel?: string;
theme?: 'azure' | 'black' | 'gray' | 'white';
}
export const useActivationCodeStore = defineStore('activationCode', () => {
const data = ref<ActivationCodeData | null>(null);
const setData = (newData: ActivationCodeData) => {
console.debug('[useActivationCodeStore] setData', newData);
data.value = newData;
};
const code = computed<string | null>(() => data.value?.code || null);
const partnerName = computed<string | null>(() => data.value?.partnerName || null);
const partnerUrl = computed<string | null>(() => data.value?.partnerUrl || null);
const partnerLogo = computed<string | null>(() =>
data.value?.partnerLogo ? `/webGui/images/partner-logo.svg` : null
);
const activationModalHidden = ref<boolean>(
sessionStorage.getItem(ACTIVATION_CODE_MODAL_HIDDEN_STORAGE_KEY) === 'true'
);
const setActivationModalHidden = (value: boolean) => (activationModalHidden.value = value);
watch(activationModalHidden, (newVal) => {
return newVal
? sessionStorage.setItem(ACTIVATION_CODE_MODAL_HIDDEN_STORAGE_KEY, 'true')
: sessionStorage.removeItem(ACTIVATION_CODE_MODAL_HIDDEN_STORAGE_KEY);
});
/**
* Should only see this if
* 1. fresh server install where no keyfile has been present before
* 2. there's not callback data
* 3. we're not on the registration page
* 4. it's not been manually hidden
*/
const showActivationModal = computed<boolean>(() => {
if (!data.value) {
return false;
}
const { callbackData } = storeToRefs(useCallbackActionsStore());
const { state } = storeToRefs(useServerStore());
const isFreshInstall = state.value === 'ENOKEYFILE';
const noCallbackData = !callbackData.value;
return isFreshInstall && noCallbackData && !activationModalHidden.value;
});
return {
code,
partnerName,
partnerUrl,
partnerLogo,
showActivationModal,
setData,
setActivationModalHidden,
};
});