xref: /openbmc/webui-vue/tests/unit/testUtils.js (revision 313d15cf9f14e8a264f97a3370c3be4e494e5e0d)
1/* eslint-env jest */
2import { createI18n } from 'vue-i18n';
3import { createStore } from 'vuex';
4
5// Create a minimal i18n instance for testing
6export function createTestI18n() {
7  return createI18n({
8    legacy: false,
9    locale: 'en-US',
10    fallbackLocale: 'en-US',
11    silentFallbackWarn: true,
12    messages: {
13      'en-US': {
14        global: {
15          table: { fromDate: 'From date', toDate: 'To date' },
16          form: {
17            fieldRequired: 'Field required',
18            invalidFormat: 'Invalid format',
19            dateMustBeBefore: 'Date must be before {date}',
20            dateMustBeAfter: 'Date must be after {date}',
21            lengthMustBeBetween: 'Length must be between {min} and {max}',
22            selectAnOption: 'Select an option',
23          },
24          action: {
25            cancel: 'Cancel',
26            save: 'Save',
27            add: 'Add',
28          },
29          status: {
30            enabled: 'Enabled',
31            disabled: 'Disabled',
32          },
33        },
34        pageUserManagement: {
35          addUser: 'Add user',
36          editUser: 'Edit user',
37          modal: {
38            accountLocked: 'Account locked',
39            clickSaveToUnlockAccount: 'Click save to unlock account',
40            unlock: 'Unlock',
41            accountStatus: 'Account status',
42            username: 'Username',
43            cannotStartWithANumber: 'Cannot start with a number',
44            noSpecialCharactersExceptUnderscore:
45              'No special characters except underscore',
46            privilege: 'Privilege',
47            userPassword: 'User password',
48            passwordMustBeBetween: 'Password must be between {min} and {max}',
49            confirmUserPassword: 'Confirm user password',
50            passwordsDoNotMatch: 'Passwords do not match',
51          },
52        },
53        pageNetwork: {
54          hostname: 'Hostname',
55          macAddress: 'MAC address',
56          modal: {
57            editHostnameTitle: 'Edit hostname',
58            editMacAddressTitle: 'Edit MAC address',
59          },
60        },
61        pageFactoryReset: {
62          modal: {
63            resetBiosSubmitText: 'Reset BIOS',
64          },
65        },
66      },
67    },
68  });
69}
70
71// Common Bootstrap Vue Next component stubs
72export const bootstrapStubs = {
73  'b-row': { template: '<div><slot /></div>' },
74  'b-col': { template: '<div><slot /></div>' },
75  'b-container': { template: '<div><slot /></div>' },
76  'b-form': {
77    template: '<form @submit.prevent="$emit(\'submit\')"><slot /></form>',
78    emits: ['submit'],
79  },
80  'b-form-group': { template: '<div><slot /></div>' },
81  'b-input-group': { template: '<div><slot /></div>' },
82  'b-form-input': {
83    template:
84      '<input :value="modelValue" @input="$emit(\'update:modelValue\', $event.target.value)" @blur="$emit(\'blur\')" @change="$emit(\'change\', $event.target.value)" />',
85    props: ['modelValue', 'state', 'type', 'id'],
86    emits: ['update:modelValue', 'blur', 'change', 'input'],
87  },
88  'b-form-select': {
89    template:
90      '<select :value="modelValue" @change="$emit(\'update:modelValue\', $event.target.value); $emit(\'change\', $event.target.value)"><slot /><slot name="first" /></select>',
91    props: ['modelValue', 'options', 'state'],
92    emits: ['update:modelValue', 'change'],
93  },
94  'b-form-select-option': { template: '<option><slot /></option>' },
95  'b-form-radio': {
96    template:
97      '<label class="form-check"><input type="radio" :value="value" :checked="modelValue === value" @change="$emit(\'update:modelValue\', value); $emit(\'change\', value)" /><slot /></label>',
98    props: ['modelValue', 'value', 'name'],
99    emits: ['update:modelValue', 'change'],
100  },
101  'b-form-checkbox': {
102    template:
103      '<label class="form-check"><input type="checkbox" :checked="modelValue" @change="$emit(\'update:modelValue\', $event.target.checked)" /><slot /></label>',
104    props: ['modelValue'],
105    emits: ['update:modelValue'],
106  },
107  'b-form-text': { template: '<div><slot /></div>' },
108  'b-form-invalid-feedback': { template: '<div><slot /></div>' },
109  'b-button': {
110    template: '<button @click="$emit(\'click\', $event)"><slot /></button>',
111    emits: ['click'],
112  },
113  'b-modal': {
114    template:
115      '<div v-if="modelValue"><slot></slot><slot name="footer" :cancel="() => $emit(\'update:modelValue\', false)"></slot></div>',
116    props: ['modelValue', 'title', 'id'],
117    emits: ['update:modelValue', 'hidden'],
118    methods: {
119      hide() {
120        this.$emit('update:modelValue', false);
121        this.$emit('hidden');
122      },
123      show() {
124        this.$emit('update:modelValue', true);
125      },
126    },
127  },
128};
129
130// Create common modal stub with refs
131export function createModalStub() {
132  return {
133    template:
134      '<div><slot></slot><slot name="footer" :cancel="() => {}"></slot></div>',
135    methods: {
136      hide: jest.fn(),
137      show: jest.fn(),
138    },
139  };
140}
141
142// Create a basic Vuex store for testing
143export function createTestStore(modules = {}) {
144  return createStore({
145    modules: {
146      global: {
147        namespaced: true,
148        getters: {
149          username: () => 'admin',
150          languagePreference: () => 'en-US',
151          serverStatus: () => 'on',
152          timezone: () => 'UTC',
153        },
154        ...modules.global,
155      },
156      ...modules,
157    },
158  });
159}
160