Skip to content

Commit

Permalink
Migrate components/resources tests to jest
Browse files Browse the repository at this point in the history
  • Loading branch information
tkleinke committed Jul 3, 2024
1 parent 6b1c312 commit d1d8ba6
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 149 deletions.
1 change: 1 addition & 0 deletions desktop/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ const config: Config = {

// The glob patterns Jest uses to detect test files
testMatch: [
'<rootDir>/test/unit/components/resources/**/*.spec.ts',
'<rootDir>/test/unit/services/**/*.spec.ts',
'<rootDir>/test/unit/util/**/*.spec.ts'
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PrintSettingsProfile } from './print-settings-profile';

const remote = window.require('@electron/remote');

const FILE_PATH = remote.getGlobal('appDataPath') + '/print-settings.json';
const FILE_PATH = remote ? remote.getGlobal('appDataPath') + '/print-settings.json' : '';


export interface PrintSettings {
Expand Down
68 changes: 38 additions & 30 deletions desktop/test/unit/components/resources/base-list.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {BaseList} from '../../../../src/app/components/resources/base-list';
import { describe, expect, test, beforeEach, jest } from '@jest/globals';
import { BaseList } from '../../../../src/app/components/resources/base-list';
import { MenuContext } from '../../../../src/app/services/menu-context';


Expand All @@ -16,67 +17,74 @@ describe('BaseList', () => {

beforeEach(() => {

viewFacade = jasmine.createSpyObj('viewFacade', ['isInExtendedSearchMode', 'navigationPathNotifications',
'getNavigationPath', 'isInOverview', 'getSelectedOperations', 'isReady']);
viewFacade.navigationPathNotifications.and.returnValue({ subscribe: () => {} });
viewFacade.getNavigationPath.and.returnValue({});
viewFacade.getSelectedOperations.and.returnValue([]);
viewFacade.isReady.and.returnValue(true);

resourcesComponent = jasmine.createSpyObj('resourcesComponent', ['getViewType']);
loading = jasmine.createSpyObj('loading', ['isLoading', 'getContext']);
menuService = jasmine.createSpyObj('menuService', ['setContext', 'getContext']);
viewFacade = {
isInExtendedSearchMode: jest.fn(),
navigationPathNotifications: jest.fn().mockReturnValue({ subscribe: () => {} }),
getNavigationPath: jest.fn().mockReturnValue({}),
isInOverview: jest.fn().mockReturnValue(true),
getSelectedOperations: jest.fn().mockReturnValue([]),
isReady: jest.fn().mockReturnValue(true)
};

resourcesComponent = {
getViewType: jest.fn(),
isEditingGeometry: false
};

loading = {
isLoading: jest.fn().mockReturnValue(false),
getContext: jest.fn()
};

menuService = {
setContext: jest.fn(),
getContext: jest.fn()
};

baseList = new BaseList(resourcesComponent, viewFacade, loading, menuService);

// partial requirements to show plus button
loading.isLoading.and.returnValue(false);
loading.getContext.and.returnValue(undefined);
viewFacade.isInOverview.and.returnValue(true);
resourcesComponent.isEditingGeometry = false;
});


it('plus button status', () => {
test('plus button status', () => {

viewFacade.isInExtendedSearchMode.and.returnValue(true);
viewFacade.isInExtendedSearchMode.mockReturnValue(true)
expect(baseList.getPlusButtonStatus()).toEqual('disabled-hierarchy');
viewFacade.isInExtendedSearchMode.and.returnValue(false);
viewFacade.isInExtendedSearchMode.mockReturnValue(false);
expect(baseList.getPlusButtonStatus()).toEqual('enabled');
});


it('plus button shown in overview', () => {
test('plus button shown in overview', () => {

expect(baseList.isPlusButtonShown()).toBeTruthy();
});


it('plus button shown if operations exist', () => {
test('plus button shown if operations exist', () => {

viewFacade.isInOverview.and.returnValue(false);
viewFacade.getSelectedOperations.and.returnValue([1]);
viewFacade.isInOverview.mockReturnValue(false);
viewFacade.getSelectedOperations.mockReturnValue([1]);
expect(baseList.isPlusButtonShown()).toBeTruthy();
});


it('plus button not shown if isEditingGeometry', () => {
test('plus button not shown if isEditingGeometry', () => {

menuService.getContext.and.returnValue(MenuContext.GEOMETRY_EDIT);
menuService.getContext.mockReturnValue(MenuContext.GEOMETRY_EDIT);
expect(baseList.isPlusButtonShown()).toBeFalsy();
});


it('plus button not shown if is loading', () => {
test('plus button not shown if is loading', () => {

loading.isLoading.and.returnValue(true);
loading.isLoading.mockReturnValue(true);
expect(baseList.isPlusButtonShown()).toBeFalsy();
});


it('plus button not shown not ready', () => {
test('plus button not shown not ready', () => {

viewFacade.isReady.and.returnValue(false);
viewFacade.isReady.mockReturnValue(false);
expect(baseList.isPlusButtonShown()).toBeFalsy();
});
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {CoordinatesUtility} from '../../../../../src/app/components/resources/map/map/coordinates-utility';
import { describe, expect, test } from '@jest/globals';
import { CoordinatesUtility } from '../../../../../src/app/components/resources/map/map/coordinates-utility';


/**
* @author Thomas Kleinke
*/
describe('CoordinatesUtility', () => {

it('convert polygon coordinates from lngLat to latLng', () => {
test('convert polygon coordinates from lngLat to latLng', () => {

const coordinates = [[[-7.0, -5.0], [-6.0, -5.0], [7.0, -7.0], [9.0, 1.0], [7.0, 7.0], [5.0, 10.0],
[-7.0, 7.0]]];
Expand All @@ -17,7 +18,8 @@ describe('CoordinatesUtility', () => {
expect(result).toEqual(expectedResult);
});

it('convert polyline coordinates from lngLat to latLng', () => {

test('convert polyline coordinates from lngLat to latLng', () => {

const coordinates = [[1.0, 3.0], [1.5, 2.5], [1.75, 2.5], [1.9, 2.25], [1.35, 2.0], [1.0, 1.0]];
const expectedResult = [[3.0, 1.0], [2.5, 1.5], [2.5, 1.75], [2.25, 1.9], [2.0, 1.35], [1.0, 1.0]];
Expand Down
48 changes: 20 additions & 28 deletions desktop/test/unit/components/resources/map/layer-manager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, expect, test, beforeEach } from '@jest/globals';
import { doc, ImageDocument } from 'idai-field-core';
import { LayerManager } from '../../../../../src/app/components/resources/map/map/layers/layer-manager';

Expand Down Expand Up @@ -30,19 +31,22 @@ describe('LayerManager', () => {

beforeEach(() => {

const mockDatastore = jasmine.createSpyObj('datastore', ['getMultiple', 'get']);
mockDatastore.getMultiple.and.returnValue(Promise.resolve(layerDocuments));
mockDatastore.get.and.returnValue(Promise.resolve(projectDocument));
const mockDatastore: any = {
getMultiple: jest.fn().mockReturnValue(Promise.resolve(layerDocuments)),
get: jest.fn().mockReturnValue(Promise.resolve(projectDocument))
};

mockViewFacade = jasmine.createSpyObj('viewFacade',
['getActiveLayersIds', 'setActiveLayersIds', 'getCurrentOperation']);
mockViewFacade.getActiveLayersIds.and.returnValue([]);
mockViewFacade = {
getActiveLayersIds: jest.fn().mockReturnValue([]),
setActiveLayersIds: jest.fn(),
getCurrentOperation: jest.fn()
};

layerManager = new LayerManager(mockDatastore, mockViewFacade, undefined);
});


it('initialize layers', async done => {
it('initialize layers', async () => {

const activeLayersChange = await layerManager.initializeLayers();

Expand All @@ -52,70 +56,58 @@ describe('LayerManager', () => {

expect(activeLayersChange.added.length).toBe(0);
expect(activeLayersChange.removed.length).toBe(0);

done();
});


it('restore active layers from resources state', async done => {
it('restore active layers from resources state', async () => {

mockViewFacade.getActiveLayersIds.and.returnValue(['l2']);
mockViewFacade.getActiveLayersIds.mockReturnValue(['l2']);

const activeLayersChange = await layerManager.initializeLayers();

expect(activeLayersChange.added.length).toBe(1);
expect(activeLayersChange.added[0]).toEqual('l2');
expect(activeLayersChange.removed.length).toBe(0);

done();
});


it('add and remove correct layers when initializing with different resources states',
async done => {
it('add and remove correct layers when initializing with different resources states', async () => {

mockViewFacade.getActiveLayersIds.and.returnValue(['l2']);
mockViewFacade.getActiveLayersIds.mockReturnValue(['l2']);

await layerManager.initializeLayers();

mockViewFacade.getActiveLayersIds.and.returnValue(['l1']);
mockViewFacade.getActiveLayersIds.mockReturnValue(['l1']);

const activeLayersChange = await layerManager.initializeLayers();

expect(activeLayersChange.added.length).toBe(1);
expect(activeLayersChange.added[0]).toEqual('l1');
expect(activeLayersChange.removed.length).toBe(1);
expect(activeLayersChange.removed[0]).toEqual('l2');

done();
});


it('add or remove no layers if the layers are initialized with the same resources state again',
async done => {
test('add or remove no layers if the layers are initialized with the same resources state again', async () => {

mockViewFacade.getActiveLayersIds.and.returnValue(['l2']);
mockViewFacade.getActiveLayersIds.mockReturnValue(['l2']);

await layerManager.initializeLayers();
const activeLayersChange = await layerManager.initializeLayers();

expect(activeLayersChange.added.length).toBe(0);
expect(activeLayersChange.removed.length).toBe(0);

done();
});


it('use default layers if no active layers ids are stored in resources state', async done => {
test('use default layers if no active layers ids are stored in resources state', async () => {

mockViewFacade.getActiveLayersIds.and.returnValue(undefined);
mockViewFacade.getActiveLayersIds.mockReturnValue(undefined);

const activeLayersChange = await layerManager.initializeLayers();

expect(activeLayersChange.added.length).toBe(1);
expect(activeLayersChange.added[0]).toEqual('l2');
expect(activeLayersChange.removed.length).toBe(0);

done();
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, expect, test, beforeEach } from '@jest/globals';
import { fieldDoc } from 'idai-field-core';
import { NavigationService } from '../../../../../src/app/components/resources/navigation/navigation-service';

Expand All @@ -12,32 +13,29 @@ describe('NavigationService', () => {

beforeEach(() => {

viewFacade = jasmine.createSpyObj(
'viewFacade',
['isInOverview', 'moveInto', 'isInExtendedSearchMode']
);
viewFacade = {
isInOverview: jest.fn().mockReturnValue(false),
moveInto: jest.fn(),
isInExtendedSearchMode: jest.fn().mockReturnValue(false)
};

projectConfiguration = jasmine.createSpyObj(
'projectConfiguration',
['getRelationsForRangeCategory', 'getCategory']
);
projectConfiguration = {
getRelationsForRangeCategory: jest.fn(),
getCategory: jest.fn()
};

messages = jasmine.createSpyObj(
'messages',
['add']
);
messages = {
add: jest.fn()
};

navigationService = new NavigationService(projectConfiguration, undefined, viewFacade, messages);

viewFacade.isInOverview.and.returnValue(false);
viewFacade.isInExtendedSearchMode.and.returnValue(false);
});


it('show jump to view buttons in overview for operation subcategories ', () => {
test('show jump to view buttons in overview for operation subcategories ', () => {

viewFacade.isInOverview.and.returnValue(true);
projectConfiguration.getCategory.and.returnValue({
viewFacade.isInOverview.mockReturnValue(true);
projectConfiguration.getCategory.mockReturnValue({
children: [ { name: 'operationSubcategory' } ]
});

Expand All @@ -47,9 +45,9 @@ describe('NavigationService', () => {
});


it('show move into buttons for resources that can be a liesWithin target', () => {
test('show move into buttons for resources that can be a liesWithin target', () => {

projectConfiguration.getRelationsForRangeCategory.and.returnValue(
projectConfiguration.getRelationsForRangeCategory.mockReturnValue(
[{ name: 'liesWithin' }]
);

Expand All @@ -59,9 +57,9 @@ describe('NavigationService', () => {
});


it('do not show move into buttons for resources that cannot be a liesWithin target', () => {
test('do not show move into buttons for resources that cannot be a liesWithin target', () => {

projectConfiguration.getRelationsForRangeCategory.and.returnValue(
projectConfiguration.getRelationsForRangeCategory.mockReturnValue(
[{ name: 'abc' }]
);

Expand All @@ -71,9 +69,9 @@ describe('NavigationService', () => {
});


it('do not show move into buttons for newly created resources without id', () => {
test('do not show move into buttons for newly created resources without id', () => {

projectConfiguration.getRelationsForRangeCategory.and.returnValue(
projectConfiguration.getRelationsForRangeCategory.mockReturnValue(
[{ name: 'liesWithin' }]
);

Expand All @@ -83,12 +81,12 @@ describe('NavigationService', () => {
});


it('do not show hierarchy buttons in extended search mode', () => {
test('do not show hierarchy buttons in extended search mode', () => {

viewFacade.isInOverview.and.returnValue(true);
viewFacade.isInExtendedSearchMode.and.returnValue(true);
viewFacade.isInOverview.mockReturnValue(true);
viewFacade.isInExtendedSearchMode.mockReturnValue(true);

projectConfiguration.getCategory.and.returnValue({
projectConfiguration.getCategory.mockReturnValue({
Operation: { children: [ { name: 'operationSubcategory' } ] }
});

Expand Down
Loading

0 comments on commit d1d8ba6

Please sign in to comment.