UNPKG

2.37 kBJavaScriptView Raw
1import React from 'react';
2import { render } from '@testing-library/react';
3import { usePreconnectLinks } from './usePreconnectLinks';
4import { ConfigProvider } from '@jetshop/core/components/ConfigProvider';
5
6const apolloConfig = {
7 graphQLURI: 'http://wow.com'
8};
9
10function expectStaticLinks() {
11 return [
12 expect.objectContaining({
13 href: apolloConfig.graphQLURI,
14 crossOrigin: null
15 }),
16 expect.objectContaining({
17 href: 'https://cdn.polyfill.io',
18 crossOrigin: null
19 })
20 ];
21}
22
23describe('usePreconnectLinks', () => {
24 test('by default returns only the base required links', () => {
25 const { links } = setup();
26
27 expect(links).toEqual(expect.arrayContaining([...expectStaticLinks()]));
28 expect(links.length).toBe(2);
29 });
30 test('if ga or gtm args are false, return base required linked', () => {
31 const { links } = setup({
32 opts: { config: Object.assign(apolloConfig, { gtm: false, ga: false }) }
33 });
34
35 expect(links).toEqual(expect.arrayContaining([...expectStaticLinks()]));
36 expect(links.length).toBe(2);
37 });
38 test('if ga is true, include ga links', () => {
39 const { links } = setup({
40 opts: { ga: true, gtm: false }
41 });
42
43 expect(links).toEqual(
44 expect.arrayContaining([
45 ...expectStaticLinks(),
46 expect.objectContaining({ href: 'https://www.google.com' })
47 ])
48 );
49 expect(links.length).toBe(5);
50 });
51 test('if gtm is true, include gtm links', () => {
52 const { links } = setup({
53 opts: { ga: false, gtm: true }
54 });
55
56 expect(links).toEqual(
57 expect.arrayContaining([
58 ...expectStaticLinks(),
59 expect.objectContaining({ href: 'https://www.googletagmanager.com' })
60 ])
61 );
62 expect(links.length).toBe(3);
63 });
64});
65
66function setup(opts = {}) {
67 let returnedLinks;
68 const utils = render(
69 <Providers config={opts.config}>
70 <TestComponent {...opts}>
71 {links => {
72 returnedLinks = links;
73
74 return null;
75 }}
76 </TestComponent>
77 </Providers>
78 );
79
80 return { ...utils, links: returnedLinks };
81}
82
83function TestComponent({ opts, children }) {
84 const links = usePreconnectLinks(opts);
85
86 return children(links);
87}
88
89function Providers(props) {
90 return (
91 <ConfigProvider config={props.config || { apolloConfig }}>
92 {props.children}
93 </ConfigProvider>
94 );
95}