Skip to content

Commit

Permalink
Test factory to create maps and nodes (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenjohanson authored Jul 15, 2024
1 parent dc42961 commit 695eabb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 118 deletions.
140 changes: 22 additions & 118 deletions teammapper-backend/src/map/controllers/maps.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { NotFoundException } from '@nestjs/common';
import { MmpMap } from '../entities/mmpMap.entity';
import { IMmpClientMap, IMmpClientPrivateMap } from '../types';
import { MmpNode } from '../entities/mmpNode.entity';
import { createClientRootNode, createMmpClientMap, createMmpMap } from '../utils/tests/mapFactories';

describe('MapsController', () => {
let mapsController: MapsController;
Expand Down Expand Up @@ -34,44 +35,15 @@ describe('MapsController', () => {

describe('duplicate', () => {
it('should duplicate a map correctly', async () => {
const oldMap: MmpMap = {
id: '6357cedd-2621-4033-8958-c50061306cb9',
adminId: 'old-admin-id',
modificationSecret: 'old-modification-secret',
name: 'Test Map',
lastModified: new Date('1970-01-01'),
options: {
fontMaxSize: 1,
fontMinSize: 1,
fontIncrement: 1
},
nodes: Array<MmpNode>()
};
const newMap: MmpMap = {
id: 'e7f66b65-ffd5-4387-b645-35f8e794c7e7',
adminId: 'new-admin-id',
modificationSecret: 'new-modification-secret',
name: 'Test Map',
lastModified: new Date('1970-01-01'),
options: {
fontMaxSize: 1,
fontMinSize: 1,
fontIncrement: 1
},
nodes: Array<MmpNode>()
};
const exportedMap: IMmpClientMap = {
uuid: 'e7f66b65-ffd5-4387-b645-35f8e794c7e7',
data: [],
deleteAfterDays: 30,
deletedAt: new Date('1970-01-01'),
lastModified: new Date('1970-01-01'),
options: {
fontMaxSize: 1,
fontMinSize: 1,
fontIncrement: 1
},
};
const oldMap: MmpMap = createMmpMap({
adminId: 'old-admin-id',
modificationSecret: 'old-modification-secret'
});
const newMap: MmpMap = createMmpMap({
adminId: 'new-admin-id',
modificationSecret: 'new-modification-secret'
});
const exportedMap: IMmpClientMap = createMmpClientMap();
const result: IMmpClientPrivateMap = {
map: exportedMap,
adminId: 'new-admin-id',
Expand Down Expand Up @@ -104,18 +76,9 @@ describe('MapsController', () => {
describe('findOne', () => {
it('should find the correct map', async () => {
const mapId = 'e7f66b65-ffd5-4387-b645-35f8e794c7e7';
const exportedMap: IMmpClientMap = {
uuid: 'e7f66b65-ffd5-4387-b645-35f8e794c7e7',
data: [],
deleteAfterDays: 30,
deletedAt: new Date('1970-01-01'),
lastModified: new Date('1970-01-01'),
options: {
fontMaxSize: 1,
fontMinSize: 1,
fontIncrement: 1
},
};
const exportedMap: IMmpClientMap = createMmpClientMap({
id: mapId,
})

jest.spyOn(mapsService, 'exportMapToClient').mockResolvedValueOnce(exportedMap);

Expand All @@ -135,19 +98,7 @@ describe('MapsController', () => {

describe('delete', () => {
it('should delete an existing map successfully', async () => {
const existingMap: MmpMap = {
id: '6357cedd-2621-4033-8958-c50061306cb9',
adminId: 'old-admin-id',
modificationSecret: 'old-modification-secret',
name: 'Test Map',
lastModified: new Date('1970-01-01'),
options: {
fontMaxSize: 1,
fontMinSize: 1,
fontIncrement: 1
},
nodes: Array<MmpNode>()
};
const existingMap: MmpMap = createMmpMap();

jest.spyOn(mapsService, 'findMap').mockResolvedValueOnce(existingMap);
// We're not interested in testing the repository at this stage, only if the request gets past the admin ID check
Expand All @@ -162,19 +113,7 @@ describe('MapsController', () => {
});

it('should not delete a map if the wrong admin ID is given', async () => {
const existingMap: MmpMap = {
id: '6357cedd-2621-4033-8958-c50061306cb9',
adminId: 'old-admin-id',
modificationSecret: 'old-modification-secret',
name: 'Test Map',
lastModified: new Date('1970-01-01'),
options: {
fontMaxSize: 1,
fontMinSize: 1,
fontIncrement: 1
},
nodes: Array<MmpNode>()
};
const existingMap: MmpMap = createMmpMap();

jest.spyOn(mapsService, 'findMap').mockResolvedValueOnce(existingMap);

Expand All @@ -189,54 +128,19 @@ describe('MapsController', () => {

describe('create', () => {
it('should create a new map if given a root node', async () => {
const newMap: MmpMap = {
id: '6357cedd-2621-4033-8958-c50061306cb9',
adminId: 'admin-id',
modificationSecret: 'modification-secret',
name: 'Test Map',
lastModified: new Date('1970-01-01'),
options: {
fontMaxSize: 1,
fontMinSize: 1,
fontIncrement: 1
},
nodes: Array<MmpNode>()
};
const exportedMap: IMmpClientMap = {
uuid: '6357cedd-2621-4033-8958-c50061306cb9',
data: [],
deleteAfterDays: 30,
deletedAt: new Date('1970-01-01'),
lastModified: new Date('1970-01-01'),
options: {
fontMaxSize: 1,
fontMinSize: 1,
fontIncrement: 1
},
};
const newMap: MmpMap = createMmpMap();

const exportedMap: IMmpClientMap = createMmpClientMap({
uuid: newMap.id,
})

const result: IMmpClientPrivateMap = {
map: exportedMap,
adminId: 'admin-id',
modificationSecret: 'modification-secret'
};

const rootNode = {
colors: {
name: '',
background: '',
branch: '',
},
font: {
style: '',
size: 0,
weight: ''
},
name: 'Root node',
image: {
src: '',
size: 0
}
};
const rootNode = createClientRootNode()

jest.spyOn(mapsService, 'createEmptyMap').mockResolvedValueOnce(newMap);
jest.spyOn(mapsService, 'exportMapToClient').mockResolvedValueOnce(exportedMap);
Expand Down
52 changes: 52 additions & 0 deletions teammapper-backend/src/map/utils/tests/mapFactories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { MmpMap } from "src/map/entities/mmpMap.entity";
import { MmpNode } from "src/map/entities/mmpNode.entity";
import * as crypto from 'crypto'
import { IMmpClientMap, IMmpClientNodeBasics } from "src/map/types";

export const createMmpMap = (overrides = {}): MmpMap => ({
id: crypto.randomUUID(),
adminId: 'admin-id',
modificationSecret: 'modification-secret',
name: 'Test Map',
lastModified: new Date('1970-01-01'),
options: {
fontMaxSize: 1,
fontMinSize: 1,
fontIncrement: 1
},
nodes: Array<MmpNode>(),
...overrides
})

export const createMmpClientMap = (overrides = {}): IMmpClientMap => ({
uuid: crypto.randomUUID(),
data: [],
deleteAfterDays: 30,
deletedAt: new Date('1970-01-01'),
lastModified: new Date('1970-01-01'),
options: {
fontMaxSize: 1,
fontMinSize: 1,
fontIncrement: 1
},
...overrides,
})

export const createClientRootNode = (overrides = {}): IMmpClientNodeBasics => ({
colors: {
name: '',
background: '',
branch: '',
},
font: {
style: '',
size: 0,
weight: ''
},
name: 'Root node',
image: {
src: '',
size: 0
},
...overrides
})

0 comments on commit 695eabb

Please sign in to comment.