Skip to content

Commit a28ab35

Browse files
bartoszherbaMarcin Kwiatkowski
authored andcommitted
test: useCurrency unit tests (#1119)
1 parent d741d00 commit a28ab35

File tree

4 files changed

+127
-2
lines changed

4 files changed

+127
-2
lines changed

packages/theme/components/CurrencySelector/CurrenciesModal.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ export default defineComponent({
4848
},
4949
props: {
5050
isModalOpen: Boolean,
51-
selectedCurrency: String,
51+
selectedCurrency: {
52+
type: String,
53+
default: '',
54+
},
5255
},
5356
emits: ['closeModal'],
5457
setup() {
@@ -58,7 +61,7 @@ export default defineComponent({
5861
load: loadCurrencies,
5962
} = useCurrency();
6063
61-
const availableCurrencies = computed(() => currencies.value?.available_currency_codes || []);
64+
const availableCurrencies = computed<string[]>(() => currencies.value?.available_currency_codes || []);
6265
6366
onMounted(() => {
6467
if (currencies.value && currencies.value?.available_currency_codes) return;
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import userEvent from '@testing-library/user-event';
2+
import { render } from '~/test-utils';
3+
import CurrenciesModal from '~/components/CurrencySelector/CurrenciesModal.vue';
4+
import { useCurrencyMock } from '~/test-utils/mocks/useCurrency';
5+
import { useCurrency } from '~/composables';
6+
import { currencyDataMock } from '~/components/CurrencySelector/__tests__/currencyData.mock';
7+
8+
jest.mock('~/composables', () => {
9+
const originalModule = jest.requireActual('~/composables');
10+
return {
11+
...originalModule,
12+
useCurrency: jest.fn(),
13+
};
14+
});
15+
16+
describe('<CurrenciesModal>', () => {
17+
it('does not render when closed', () => {
18+
(useCurrency as jest.Mock).mockReturnValue(useCurrencyMock);
19+
const { queryByRole } = render(CurrenciesModal, {
20+
props: {
21+
isModalOpen: false,
22+
},
23+
});
24+
25+
expect(queryByRole('heading', { name: /change store/i })).toBeNull();
26+
});
27+
28+
it('emits "closeModal" event on close', async () => {
29+
const user = userEvent.setup();
30+
31+
(useCurrency as jest.Mock).mockReturnValue(useCurrencyMock);
32+
const { getAllByRole, emitted } = render(CurrenciesModal, {
33+
props: {
34+
isModalOpen: true,
35+
},
36+
});
37+
38+
const closeBtn = getAllByRole('button', { name: /close/i })[0];
39+
40+
await user.click(closeBtn);
41+
42+
expect(emitted()).toHaveProperty('closeModal');
43+
});
44+
45+
it('renders list of available stores', () => {
46+
useCurrencyMock.currency.value = currencyDataMock;
47+
48+
(useCurrency as jest.Mock).mockReturnValue(useCurrencyMock);
49+
50+
const { getByRole, getByText } = render(CurrenciesModal, {
51+
props: {
52+
isModalOpen: true,
53+
},
54+
});
55+
56+
expect(getByRole('heading', { name: /choose currency/i })).toBeTruthy();
57+
expect(getByText(/eur/i)).toBeTruthy();
58+
expect(getByText(/pln/i)).toBeTruthy();
59+
expect(getByText(/usd/i)).toBeTruthy();
60+
});
61+
62+
it('on currency selection executes method that must trigger currency switch', async () => {
63+
const user = userEvent.setup();
64+
useCurrencyMock.currency.value = currencyDataMock;
65+
66+
(useCurrency as jest.Mock).mockReturnValue(useCurrencyMock);
67+
68+
const { getByText } = render(CurrenciesModal, {
69+
props: {
70+
isLangModalOpen: true,
71+
storeConfig: { store_code: 'default' },
72+
},
73+
});
74+
75+
const eurSwitchBtn = getByText(/eur/i);
76+
await user.click(eurSwitchBtn);
77+
78+
expect(useCurrencyMock.change).toBeCalledWith({ id: 'EUR' });
79+
});
80+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export const currencyDataMock = {
2+
__typename: 'Currency',
3+
available_currency_codes: [
4+
'EUR',
5+
'PLN',
6+
'USD',
7+
],
8+
base_currency_code: 'USD',
9+
base_currency_symbol: '$',
10+
default_display_currecy_code: null,
11+
default_display_currecy_symbol: null,
12+
default_display_currency_code: 'USD',
13+
default_display_currency_symbol: '$',
14+
exchange_rates: [
15+
{
16+
__typename: 'ExchangeRate',
17+
currency_to: 'EUR',
18+
rate: 0.93,
19+
},
20+
{
21+
__typename: 'ExchangeRate',
22+
currency_to: 'PLN',
23+
rate: 4.29,
24+
},
25+
{
26+
__typename: 'ExchangeRate',
27+
currency_to: 'USD',
28+
rate: 1,
29+
},
30+
],
31+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { ref } from '@nuxtjs/composition-api';
2+
3+
export const useCurrencyMock = {
4+
currency: ref({}),
5+
load: jest.fn(),
6+
change: jest.fn(),
7+
error: ref({
8+
load: null,
9+
change: null,
10+
}),
11+
};

0 commit comments

Comments
 (0)