Advertisement
ahmadandika

map.test.js

Mar 25th, 2021
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import { mount } from 'enzyme';
  3. import { MapContainer } from 'react-leaflet';
  4. import { Map } from './Map';
  5. import * as mapService from '../../../../services/map.service';
  6.  
  7. const streamingAPISpy = jest.spyOn(mapService, 'streamingAPI');
  8.  
  9. describe('<Map />', () => {
  10.   let component;
  11.   let useEffect;
  12.  
  13.   const mockUseEffect = () => {
  14.     useEffect.mockImplementationOnce((f) => f());
  15.   };
  16.  
  17.   beforeEach(() => {
  18.     useEffect = jest.spyOn(React, 'useEffect');
  19.     // useEffect = jest.spyOn(React, "useEffect").mockImplementation(f => f());{
  20.     jest.restoreAllMocks();
  21.     jest.resetAllMocks();
  22.  
  23.     mockUseEffect();
  24.  
  25.     component = mount(
  26.       <MapContainer>
  27.         <Map />
  28.       </MapContainer>,
  29.     );
  30.   });
  31.  
  32.   afterEach(() => {
  33.     streamingAPISpy.mockClear();
  34.   });
  35.  
  36.   test('It should mount', () => {
  37.     expect(component.length).toBe(1);
  38.   });
  39.  
  40.   test('should render from stream', async () => {
  41.     streamingAPISpy.mockImplementationOnce(() =>
  42.       Promise.resolve({
  43.         e: { data: '/fleettrack/realtime/' },
  44.       }),
  45.     );
  46.     await streamingAPISpy('/api');
  47.     component.update();
  48.     expect(component.length).toBe(1);
  49.  
  50.     expect(streamingAPISpy).toHaveBeenCalledWith('/api');
  51.   });
  52.  
  53.   test('should listen', () => {
  54.     const onmessage = jest.fn().mockReturnValue({
  55.       e: { data: '/fleettrack/realtime/' },
  56.     });
  57.  
  58.     const useRefSpy = jest.spyOn(React, 'useRef').mockReturnValue({
  59.       current: {
  60.         sse: {
  61.           onmessage,
  62.         },
  63.       },
  64.     });
  65.     expect(useRefSpy).toBeCalledTimes('1');
  66.   });
  67. });
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement