-
Notifications
You must be signed in to change notification settings - Fork 3
/
manifold-product-card.spec.ts
70 lines (58 loc) · 2.35 KB
/
manifold-product-card.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { newSpecPage } from '@stencil/core/testing';
import fetchMock from 'fetch-mock';
import { ManifoldProductCard } from './manifold-product-card';
import { ManifoldProductCardView } from '../manifold-product-card-view/manifold-product-card-view';
import { createGraphqlFetch } from '../../utils/graphqlFetch';
import product from '../../spec/mock/logdna/product';
const graphqlEndpoint = 'http://test.com/graphql';
interface Props {
preserveEvent?: boolean;
productLabel?: string;
productLinkFormat?: string;
}
async function setup(props: Props) {
const page = await newSpecPage({
components: [ManifoldProductCard, ManifoldProductCardView],
html: '<div></div>',
});
const component = page.doc.createElement('manifold-product-card');
component.graphqlFetch = createGraphqlFetch({
endpoint: () => graphqlEndpoint,
});
component.productLabel = props.productLabel;
component.productLinkFormat = props.productLinkFormat;
component.preserveEvent = props.preserveEvent;
const root = page.root as HTMLDivElement;
root.appendChild(component);
await page.waitForChanges();
return { page, component };
}
describe('<manifold-product-card>', () => {
beforeEach(async () => fetchMock.mock(graphqlEndpoint, { data: { product } }));
afterEach(fetchMock.restore);
describe('v0 props', () => {
it('[product-label]: fetches if given', async () => {
await setup({ productLabel: 'new-product' });
expect(fetchMock.called(`begin:${graphqlEndpoint}`)).toBe(true);
});
it('[product-link-format]: formats links correctly', async () => {
const { page } = await setup({
productLabel: product.label,
productLinkFormat: '/product/:product',
});
const view = page.root && page.root.querySelector('manifold-product-card-view');
const link = view && view.shadowRoot && view.shadowRoot.querySelector('a');
const href = link && link.getAttribute('href');
expect(href).toBe(`/product/${product.label}`);
});
it('[preserve-event]: it passes result to child', async () => {
const { page } = await setup({
productLabel: 'my-product',
productLinkFormat: '/product/:product',
preserveEvent: true,
});
const view = page.root && page.root.querySelector('manifold-product-card-view');
expect(view && view.preserveEvent).toBe(true);
});
});
});