Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

5.8 KB
Raw Blame
import { TextEncoder, TextDecoder } from 'util';
import configureMockStore from 'redux-mock-store';
import { thunk } from 'redux-thunk';
import {
  openBasicContent,
  openCustomContent,
  renderTemplate,
  requestTemplate,
  onToggle,
  onClose,
  toggleAboutModal,
  toggleCustomContent,
} from './actions';
import * as constants from './constants';
import util from '../../util/util';

global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;

const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
const ERROR_MESSAGE = 'There was an error';

describe('Modal open actions', () => {
  test(
    `openBasicContent action returns ${
      constants.OPEN_BASIC
    }header text, body text and encode key [modal-actions-open]`,
    () => {
      const expectedAction = {
        type: constants.OPEN_BASIC,
        headerText: constants.TEST_HEADER,
        bodyText: constants.TEST_BODY,
        key: util.encodeId(`__BASIC_MODAL__${constants.TEST_HEADER}`),
      };
      expect(
        openBasicContent(constants.TEST_HEADER, constants.TEST_BODY),
      ).toEqual(expectedAction);
    },
  );

  test(`onToggle action returns ${constants.TOGGLE}as type [modal-actions-toggle]`, () => {
    const expectedAction = {
      type: constants.TOGGLE,
    };
    expect(onToggle()).toEqual(expectedAction);
  });

  test(
    `openCustomContent action returns ${
      constants.OPEN_CUSTOM
    } action type [modal-actions-open-custom]`,
    () => {
      const customsKey = 'CUSTOM_MODAL_KEY';
      const customsParams = {
        offsetRight: '70px',
        headerText: constants.TEST_HEADER,
        modalClassName:
          'toolbar-snapshot-modal toolbar-modal toolbar-medium-modal',
      };
      const expectedAction = {
        type: constants.OPEN_CUSTOM,
        key: customsKey,
        customProps: customsParams,
      };
      expect(openCustomContent(customsKey, customsParams)).toEqual(
        expectedAction,
      );
    },
  );

  test(
    `renderTemplate action returns ${constants.RENDER_TEMPLATE} type [modal-actions-render-template]`,
    () => {
      const templateModalKey = 'somePageName';
      const expectedAction = {
        type: constants.RENDER_TEMPLATE,
        key: util.encodeId(templateModalKey),
        template: templateModalKey,
        headerText: constants.TEST_HEADER,
      };
      expect(renderTemplate(constants.TEST_HEADER, templateModalKey)).toEqual(
        expectedAction,
      );
    },
  );

  test(`onClose action returns ${constants.CLOSE} as type [modal-actions-close]`, () => {
    const expectedAction = {
      type: constants.CLOSE,
    };
    expect(onClose()).toEqual(expectedAction);
  });

  test(`toggleAboutModal returns ${constants.OPEN_ABOUT} when isOpen is true [modal-actions-open-about]`, () => {
    const expectedAction = {
      type: constants.OPEN_ABOUT,
    };
    expect(toggleAboutModal(true)).toEqual(expectedAction);
  });

  test(`toggleAboutModal returns ${constants.CLOSE_ABOUT} when isOpen is false [modal-actions-close-about]`, () => {
    const expectedAction = {
      type: constants.CLOSE_ABOUT,
    };
    expect(toggleAboutModal(false)).toEqual(expectedAction);
  });
});

describe('toggleCustomContent actions', () => {
  test(`dispatches ${constants.CLOSE} when modal is open with same key [modal-actions-toggle-custom-close]`, () => {
    const key = 'CUSTOM_MODAL_KEY';
    const params = { headerText: constants.TEST_HEADER };
    const store = mockStore({ modal: { id: key, isOpen: true } });
    store.dispatch(toggleCustomContent(key, params));
    expect(store.getActions()).toEqual([{ type: constants.CLOSE }]);
  });

  test(`dispatches ${constants.OPEN_CUSTOM} when modal is closed [modal-actions-toggle-custom-open]`, () => {
    const key = 'CUSTOM_MODAL_KEY';
    const params = { headerText: constants.TEST_HEADER };
    const store = mockStore({ modal: { id: key, isOpen: false } });
    store.dispatch(toggleCustomContent(key, params));
    expect(store.getActions()).toEqual([
      {
        type: constants.OPEN_CUSTOM,
        key,
        customProps: params,
      },
    ]);
  });

  test(`dispatches ${constants.OPEN_CUSTOM} when modal key differs [modal-actions-toggle-custom-different-key]`, () => {
    const key = 'CUSTOM_MODAL_KEY';
    const differentKey = 'DIFFERENT_MODAL_KEY';
    const params = { headerText: constants.TEST_HEADER };
    const store = mockStore({ modal: { id: differentKey, isOpen: true } });
    store.dispatch(toggleCustomContent(key, params));
    expect(store.getActions()).toEqual([
      {
        type: constants.OPEN_CUSTOM,
        key,
        customProps: params,
      },
    ]);
  });
});

describe('Template fetching', () => {
  beforeEach(() => {
    fetch.resetMocks();
  });

  test('triggers start and success action types [modal-actions-success]', () => {
    const loc = 'mock/';
    fetch.mockResponseOnce(constants.ABOUT_MOCK_RESPONSE);
    const expectedActions = [
      { type: constants.TEMPLATE_REQUEST_START },
      {
        type: constants.TEMPLATE_REQUEST_SUCCESS,
        response: constants.ABOUT_MOCK_RESPONSE,
      },
    ];
    const store = mockStore({ modal: {} });
    return store
      .dispatch(requestTemplate(constants.TEMPLATE_REQUEST, loc, 'text/html'))
      .then(() => {
        expect(store.getActions()).toEqual(expectedActions);
      });
  });

  test(`creates ${constants.TEMPLATE_REQUEST_FAILURE} Action [modal-actions-failure]`, () => {
    const loc = 'mock/';
    fetch.mockRejectOnce(ERROR_MESSAGE);
    const expectedActions = [
      { type: constants.TEMPLATE_REQUEST_START },
      {
        type: constants.TEMPLATE_REQUEST_FAILURE,
        error: ERROR_MESSAGE,
      },
    ];
    const store = mockStore({ modal: {} });
    return store
      .dispatch(requestTemplate(constants.TEMPLATE_REQUEST, loc, 'text/html'))
      .catch(() => {
        expect(store.getActions()).toEqual(expectedActions);
      });
  });
});