Skip to content

Commit df61a39

Browse files
committed
test: add global vue/nuxt mocks for unit tests
1 parent 59f4762 commit df61a39

File tree

3 files changed

+61
-57
lines changed

3 files changed

+61
-57
lines changed

packages/theme/jest-setup.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,35 @@
11
import '@testing-library/jest-dom';
22
import Vue from 'vue';
3+
import { config } from '@vue/test-utils';
4+
5+
config.stubs = {
6+
NuxtImg: { render(h) { return h('img'); } },
7+
};
8+
9+
const $vsf = {
10+
$magento: {
11+
config: {
12+
imageProvider: '',
13+
magentoBaseUrl: '',
14+
state: {},
15+
},
16+
},
17+
};
18+
19+
// mocks object returned by useContext()
20+
Vue.prototype.$nuxt = {
21+
context: {
22+
$vsf,
23+
app: {
24+
// $vsf intentionally doubled in context top level AND in context.app - this is the way it's in the app
25+
$vsf,
26+
$fc: jest.fn((label) => label),
27+
localePath: jest.fn((link) => link),
28+
},
29+
i18n: {
30+
t: jest.fn((label) => label),
31+
},
32+
},
33+
};
334

435
Vue.directive('e2e', {});

packages/theme/modules/catalog/category/components/views/__tests__/CategoryProductGrid.spec.ts

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,6 @@ import { productsMock } from './productsMock';
77

88
const localVue = createLocalVue();
99
localVue.use(PiniaVuePlugin);
10-
localVue.component('NuxtImg', { render(h) { return h('div'); } });
11-
12-
localVue.prototype.$nuxt = {
13-
context: {
14-
$vsf: {
15-
$magento: {
16-
config: {
17-
imageProvider: '',
18-
magentoBaseUrl: '',
19-
},
20-
},
21-
},
22-
app: {
23-
$fc: jest.fn((label) => label),
24-
localePath: jest.fn(),
25-
$vsf: {
26-
$magento: {
27-
config: {
28-
state: '',
29-
},
30-
},
31-
},
32-
},
33-
i18n: {
34-
t: jest.fn((label) => label),
35-
},
36-
},
37-
};
3810

3911
describe('CategoryProductGrid', () => {
4012
it('shows skeleton loader when loading', async () => {
@@ -44,7 +16,14 @@ describe('CategoryProductGrid', () => {
4416
});
4517

4618
it('shows products when loaded', async () => {
47-
const { findAllByTestId } = render(CategoryProductGrid, { props: { loading: false, products: productsMock }, localVue, pinia: createTestingPinia() });
19+
const { findAllByTestId } = render(CategoryProductGrid, {
20+
props: {
21+
loading: false,
22+
products: productsMock,
23+
},
24+
localVue,
25+
pinia: createTestingPinia(),
26+
});
4827
const products = await findAllByTestId('product-card');
4928
expect(products).toHaveLength(productsMock.length);
5029
});

packages/theme/modules/catalog/category/components/views/__tests__/CategoryProductList.spec.ts

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,32 @@ import CategoryProductList from '../CategoryProductList.vue';
77

88
const localVue = createLocalVue();
99
localVue.use(PiniaVuePlugin);
10-
localVue.component('NuxtImg', { render(h) { return h('div'); } });
11-
12-
localVue.prototype.components = {
13-
components: {
14-
NuxtImg: 'div',
15-
},
16-
};
17-
18-
localVue.prototype.$nuxt = {
19-
context: {
20-
app: {
21-
$vsf: {
22-
$magento: {
23-
config: {
24-
state: '',
25-
},
26-
},
27-
},
28-
},
29-
i18n: {
30-
t: jest.fn((label) => label),
31-
},
32-
},
33-
};
3410

3511
describe('CategoryProductList', () => {
36-
it('hides \'Add to wishlist\' button when logged out', () => {});
37-
38-
it('picks correct label for wishlist button', () => {});
12+
it.each([
13+
[true, true],
14+
[false, false],
15+
])('has correct \'Add to wishlist\' button visiblity when loggin state is %s', (isLoggedIn, expectedVisibility) => {
16+
const { queryByTestId } = render(CategoryProductList, {
17+
props: {
18+
loading: false,
19+
products: [productsMock[0]],
20+
},
21+
localVue,
22+
pinia: createTestingPinia({ initialState: { customer: { isLoggedIn } } }),
23+
});
24+
expect(Boolean(queryByTestId('wishlist-button'))).toBe(expectedVisibility);
25+
});
3926

4027
it('shows skeleton loader when loading', async () => {
41-
const { findAllByTestId } = render(CategoryProductList, { props: { loading: true, products: [] }, localVue, pinia: createTestingPinia() });
28+
const { findAllByTestId } = render(CategoryProductList, {
29+
props: {
30+
loading: true,
31+
products: [],
32+
},
33+
localVue,
34+
pinia: createTestingPinia(),
35+
});
4236
const loadingSkeletons = await findAllByTestId('skeleton');
4337
expect(loadingSkeletons).not.toHaveLength(0);
4438
});

0 commit comments

Comments
 (0)