You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PageTracker-test.tsx 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { mount } from 'enzyme';
  21. import * as React from 'react';
  22. import { PageTracker } from '../PageTracker';
  23. import { mockLocation, mockRouter } from '../../../helpers/testMocks';
  24. jest.useFakeTimers();
  25. beforeEach(() => {
  26. jest.clearAllTimers();
  27. (window as any).dataLayer = [];
  28. document.getElementsByTagName = jest.fn().mockImplementation(() => {
  29. return [document.body];
  30. });
  31. });
  32. it('should not trigger if no analytics system is given', () => {
  33. shallowRender();
  34. expect(setTimeout).not.toHaveBeenCalled();
  35. });
  36. it('should work for Google Analytics', () => {
  37. const wrapper = shallowRender({ trackingIdGA: '123' });
  38. const instance = wrapper.instance();
  39. instance.trackPage();
  40. expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 500);
  41. });
  42. it('should work for Google Tag Manager', () => {
  43. const wrapper = shallowRender({ trackingIdGTM: '123' });
  44. const instance = wrapper.instance();
  45. const dataLayer = (window as any).dataLayer;
  46. expect(dataLayer).toHaveLength(1);
  47. instance.trackPage();
  48. expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 500);
  49. jest.runAllTimers();
  50. expect(dataLayer).toHaveLength(2);
  51. });
  52. function shallowRender(props: Partial<PageTracker['props']> = {}) {
  53. return mount<PageTracker>(
  54. <PageTracker
  55. location={mockLocation()}
  56. params={{}}
  57. router={mockRouter()}
  58. routes={[]}
  59. {...props}
  60. />
  61. );
  62. }