playlist_srv.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { PlaylistSrv } from '../playlist_srv';
  2. const dashboards = [{ uri: 'dash1' }, { uri: 'dash2' }];
  3. const createPlaylistSrv = (): [PlaylistSrv, { url: jest.MockInstance<any> }] => {
  4. const mockBackendSrv = {
  5. get: jest.fn(url => {
  6. switch (url) {
  7. case '/api/playlists/1':
  8. return Promise.resolve({ interval: '1s' });
  9. case '/api/playlists/1/dashboards':
  10. return Promise.resolve(dashboards);
  11. default:
  12. throw new Error(`Unexpected url=${url}`);
  13. }
  14. }),
  15. };
  16. const mockLocation = {
  17. url: jest.fn(),
  18. search: () => ({}),
  19. };
  20. const mockTimeout = jest.fn();
  21. (mockTimeout as any).cancel = jest.fn();
  22. return [new PlaylistSrv(mockLocation, mockTimeout, mockBackendSrv), mockLocation];
  23. };
  24. const mockWindowLocation = (): [jest.MockInstance<any>, () => void] => {
  25. const oldLocation = window.location;
  26. const hrefMock = jest.fn();
  27. // JSDom defines window in a way that you cannot tamper with location so this seems to be the only way to change it.
  28. // https://github.com/facebook/jest/issues/5124#issuecomment-446659510
  29. delete window.location;
  30. window.location = {} as any;
  31. // Only mocking href as that is all this test needs, but otherwise there is lots of things missing, so keep that
  32. // in mind if this is reused.
  33. Object.defineProperty(window.location, 'href', {
  34. set: hrefMock,
  35. get: hrefMock,
  36. });
  37. const unmock = () => {
  38. window.location = oldLocation;
  39. };
  40. return [hrefMock, unmock];
  41. };
  42. describe('PlaylistSrv', () => {
  43. let srv: PlaylistSrv;
  44. let mockLocationService: { url: jest.MockInstance<any> };
  45. let hrefMock: jest.MockInstance<any>;
  46. let unmockLocation: () => void;
  47. const initialUrl = 'http://localhost/playlist';
  48. beforeEach(() => {
  49. [srv, mockLocationService] = createPlaylistSrv();
  50. [hrefMock, unmockLocation] = mockWindowLocation();
  51. // This will be cached in the srv when start() is called
  52. hrefMock.mockReturnValue(initialUrl);
  53. });
  54. afterEach(() => {
  55. unmockLocation();
  56. });
  57. it('runs all dashboards in cycle and reloads page after 3 cycles', async () => {
  58. await srv.start(1);
  59. for (let i = 0; i < 6; i++) {
  60. expect(mockLocationService.url).toHaveBeenLastCalledWith(`dashboard/${dashboards[i % 2].uri}?`);
  61. srv.next();
  62. }
  63. expect(hrefMock).toHaveBeenCalledTimes(2);
  64. expect(hrefMock).toHaveBeenLastCalledWith(initialUrl);
  65. });
  66. it('keeps the refresh counter value after restarting', async () => {
  67. await srv.start(1);
  68. // 1 complete loop
  69. for (let i = 0; i < 3; i++) {
  70. expect(mockLocationService.url).toHaveBeenLastCalledWith(`dashboard/${dashboards[i % 2].uri}?`);
  71. srv.next();
  72. }
  73. srv.stop();
  74. await srv.start(1);
  75. // Another 2 loops
  76. for (let i = 0; i < 4; i++) {
  77. expect(mockLocationService.url).toHaveBeenLastCalledWith(`dashboard/${dashboards[i % 2].uri}?`);
  78. srv.next();
  79. }
  80. expect(hrefMock).toHaveBeenCalledTimes(3);
  81. expect(hrefMock).toHaveBeenLastCalledWith(initialUrl);
  82. });
  83. });