1 | import React from 'react';
|
2 | import { render } from '@testing-library/react';
|
3 | import { usePreconnectLinks } from './usePreconnectLinks';
|
4 | import { ConfigProvider } from '@jetshop/core/components/ConfigProvider';
|
5 |
|
6 | const apolloConfig = {
|
7 | graphQLURI: 'http://wow.com'
|
8 | };
|
9 |
|
10 | function 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 |
|
23 | describe('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 |
|
66 | function 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 |
|
83 | function TestComponent({ opts, children }) {
|
84 | const links = usePreconnectLinks(opts);
|
85 |
|
86 | return children(links);
|
87 | }
|
88 |
|
89 | function Providers(props) {
|
90 | return (
|
91 | <ConfigProvider config={props.config || { apolloConfig }}>
|
92 | {props.children}
|
93 | </ConfigProvider>
|
94 | );
|
95 | }
|