Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
map.thunks.test.ts 4.96 KiB
import { MODELS_MOCK } from '@/models/mocks/modelsMock';
/* eslint-disable no-magic-numbers */
import { QueryData } from '@/types/query';
import { BACKGROUNDS_MOCK, BACKGROUND_INITIAL_STATE_MOCK } from '../backgrounds/background.mock';
import { RootState } from '../store';
import { INITIAL_STORE_STATE_MOCK } from '../root/root.fixtures';
import { MODELS_INITIAL_STATE_MOCK } from '../models/models.mock';
import {
  getBackgroundId,
  getInitMapPosition,
  getInitMapSizeAndModelId,
  getOpenedMaps,
} from './map.thunks';
import { initialMapDataFixture, initialMapStateFixture } from './map.fixtures';

const EMPTY_QUERY_DATA: QueryData = {
  modelId: undefined,
  backgroundId: undefined,
  initialPosition: undefined,
  perfectMatch: false,
};

const QUERY_DATA_WITH_BG: QueryData = {
  modelId: undefined,
  backgroundId: 21,
  initialPosition: undefined,
  perfectMatch: false,
};

const QUERY_DATA_WITH_MODELID: QueryData = {
  modelId: 5054,
  backgroundId: undefined,
  initialPosition: undefined,
  perfectMatch: false,
};

const QUERY_DATA_WITH_POSITION: QueryData = {
  modelId: undefined,
  backgroundId: undefined,
  initialPosition: {
    x: 21,
    y: 3,
    z: 7,
  },
  perfectMatch: false,
};

const STATE_WITH_MODELS: RootState = {
  ...INITIAL_STORE_STATE_MOCK,
  models: { ...MODELS_INITIAL_STATE_MOCK, data: MODELS_MOCK },
};

describe('map thunks - utils', () => {
  describe('getBackgroundId', () => {
    it('should return backgroundId value from queryData', () => {
      const backgroundId = getBackgroundId(INITIAL_STORE_STATE_MOCK, QUERY_DATA_WITH_BG);
      expect(backgroundId).toBe(21);
    });
    it('should return main map background id if query param does not include background id', () => {
      const store: RootState = {
        ...INITIAL_STORE_STATE_MOCK,
        backgrounds: { ...BACKGROUND_INITIAL_STATE_MOCK, data: BACKGROUNDS_MOCK },
      };
      const backgroundId = getBackgroundId(store, EMPTY_QUERY_DATA);

      expect(backgroundId).toBe(13);
    });
    it('should return default value (0) if query data does not include backgroundId and could not find main background in the store', () => {
      const backgroundId = getBackgroundId(INITIAL_STORE_STATE_MOCK, EMPTY_QUERY_DATA);
      expect(backgroundId).toBe(0);
    });
  });

  describe('getInitMapPosition', () => {
    it('should return valid map position from query params ', () => {
      const position = getInitMapPosition(STATE_WITH_MODELS, QUERY_DATA_WITH_POSITION);
      expect(position).toEqual({
        initial: { x: 21, y: 3, z: 7 },
        last: { x: 21, y: 3, z: 7 },
      });
    });

    it('should return valid map position if query params do not include position', () => {
      const position = getInitMapPosition(STATE_WITH_MODELS, EMPTY_QUERY_DATA);
      expect(position).toEqual({
        initial: { x: 13389.625, y: 6751.5, z: 5 },
        last: { x: 13389.625, y: 6751.5, z: 5 },
      });
    });
    it('should return default map position', () => {
      const position = getInitMapPosition(INITIAL_STORE_STATE_MOCK, EMPTY_QUERY_DATA);

      expect(position).toEqual({ initial: { x: 0, y: 0, z: 0 }, last: { x: 0, y: 0, z: 0 } });
    });
  });

  describe('getInitMapSizeAndModelId', () => {
    it('should return correct mapsize and modelid when modelId is provided in queryData', () => {
      const payload = getInitMapSizeAndModelId(STATE_WITH_MODELS, QUERY_DATA_WITH_MODELID);

      expect(payload).toEqual({
        modelId: 5054,
        size: { height: 1171.9429798877356, maxZoom: 5, minZoom: 2, tileSize: 256, width: 1652.75 },
      });
    });
    it('should return correct mapsize and modelId if query params do not include modelId', () => {
      const payload = getInitMapSizeAndModelId(STATE_WITH_MODELS, EMPTY_QUERY_DATA);
      expect(payload).toEqual({
        modelId: 5053,
        size: {
          height: 13503,
          maxZoom: 9,
          minZoom: 2,
          tileSize: 256,
          width: 26779.25,
        },
      });
    });
  });

  describe('getOpenedMaps ', () => {
    it('should return main map only', () => {
      const openedMaps = getOpenedMaps(
        {
          ...STATE_WITH_MODELS,
          map: { ...initialMapStateFixture, data: { ...initialMapDataFixture, modelId: 5053 } },
        },
        EMPTY_QUERY_DATA,
      );

      expect(openedMaps).toEqual([
        { lastPosition: { x: 0, y: 0, z: 0 }, modelId: 5053, modelName: 'Main map' },
      ]);
    });
    it('should return main map and opened submap', () => {
      const openedMaps = getOpenedMaps(
        {
          ...STATE_WITH_MODELS,
          map: { ...initialMapStateFixture, data: { ...initialMapDataFixture, modelId: 5054 } },
        },
        EMPTY_QUERY_DATA,
      );

      expect(openedMaps).toEqual([
        { lastPosition: { x: 0, y: 0, z: 0 }, modelId: 5053, modelName: 'Main map' },
        {
          lastPosition: {
            x: 0,
            y: 0,
            z: 0,
          },
          modelId: 5054,
          modelName: 'PRKN substrates',
        },
      ]);
    });
  });
});