Skip to content

Commit

Permalink
chore: adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Conrad Chan committed Apr 16, 2020
1 parent 6ac0385 commit e22156f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/@types/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable flowtype/no-types-missing-file-annotation, no-use-before-define */
import { Permissions } from './api';

// New Data Model Types
export interface Annotation {
Expand Down
2 changes: 1 addition & 1 deletion src/region/RegionContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import {
ApplicationState,
CreatorItem,
CreatorStatus,
getActiveAnnotationId,
getAnnotationMode,
getAnnotationsForLocation,
getCreatorStagedForLocation,
getCreatorStatus,
setStagedAction,
setStatusAction,
getActiveAnnotationId,
} from '../store';
import { createRegionAction } from './actions';
import RegionAnnotations from './RegionAnnotations';
Expand Down
29 changes: 29 additions & 0 deletions src/region/__mocks__/data.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/camelcase */
export const annotations = [
{ id: 'anno_1', target: { shape: { height: 10, width: 10, x: 10, y: 10, type: 'rect' }, type: 'region' } },
{ id: 'anno_2', target: { shape: { height: 20, width: 20, x: 20, y: 20, type: 'rect' }, type: 'region' } },
Expand All @@ -19,3 +20,31 @@ export const target = {
shape: rect,
type: 'region' as const,
};

export const annotation = {
created_at: new Date(),
created_by: {
id: '1',
login: 'johndoe',
name: 'John Doe',
type: 'user' as const,
},
id: '123',
modified_at: new Date(),
modified_by: {
id: '1',
login: 'johndoe',
name: 'John Doe',
type: 'user' as const,
},
permissions: {
can_delete: true,
can_edit: true,
},
target: {
location: { type: 'page' as const, value: 1 },
shape: { height: 10, width: 10, x: 10, y: 10, type: 'rect' as const },
type: 'region' as const,
},
type: 'annotation' as const,
};
13 changes: 13 additions & 0 deletions src/region/__tests__/RegionAnnotations-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,18 @@ describe('RegionAnnotations', () => {
expect(wrapper.exists(RegionAnnotation)).toBe(false);
expect(wrapper.exists(RegionCreator)).toBe(false);
});

test('should render the specified annotation based on activeAnnotationId', () => {
const wrapper = getWrapper({ activeAnnotationId: 'anno_1', annotations });
const renderedAnnotations = wrapper.find(RegionAnnotation);

expect(wrapper.exists('.ba-RegionAnnotations-list')).toBe(true);
expect(renderedAnnotations.length).toBe(3);

for (let i = 0; i < 3; i += 1) {
expect(renderedAnnotations.get(i).key).toBe(annotations[i].id);
expect(renderedAnnotations.get(i).props.isActive).toBe(i === 0);
}
});
});
});
1 change: 1 addition & 0 deletions src/region/__tests__/RegionContainer-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('RegionContainer', () => {

expect(wrapper.exists('RootProvider')).toBe(true);
expect(wrapper.find(RegionAnnotations).props()).toMatchObject({
activeAnnotationId: null,
annotations: [],
createRegion: expect.any(Function),
isCreating: false,
Expand Down
6 changes: 4 additions & 2 deletions src/store/eventing/__tests__/create-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Action } from '@reduxjs/toolkit';
import { handleCreateErrorEvents, handleCreatePendingEvents, handleCreateSuccessEvents } from '../create';
import eventManager from '../../../common/EventManager';
import { ApplicationState } from '../../types';
import { annotation as payload } from '../../../region/__mocks__/data';

jest.mock('../../../common/EventManager');

Expand Down Expand Up @@ -51,10 +52,11 @@ describe('store/eventing/create', () => {

describe('handleCreateSuccessEvents()', () => {
test('should emit create event with success status', () => {
handleCreateSuccessEvents(prevState, nextState, action);
const actionWithPayload = { ...action, payload };
handleCreateSuccessEvents(prevState, nextState, actionWithPayload);

expect(eventManager.emit).toHaveBeenLastCalledWith('annotations_create', {
annotation,
annotation: payload,
meta: {
status: 'success',
},
Expand Down

0 comments on commit e22156f

Please sign in to comment.