Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
fix: comment test
Browse files Browse the repository at this point in the history
  • Loading branch information
instalando committed Aug 25, 2023
1 parent 3a88646 commit e91bb25
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 30 deletions.
77 changes: 63 additions & 14 deletions src/components/comments/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ import ApiService from '../../services/api';

import { CommentsComponent } from './index';

jest.mock('../../services/api', () => ({
fetchAnnotation: jest.fn().mockImplementation((): any => []),
createAnnotations: jest.fn().mockImplementation(() => []),
createComment: jest.fn().mockImplementation(() => []),
resolveAnnotation: jest.fn().mockImplementation(() => []),
}));

describe('CommentsComponent', () => {
let commentsComponent: CommentsComponent;

beforeEach(() => {
jest.clearAllMocks();
commentsComponent = new CommentsComponent();

commentsComponent.attach({
Expand All @@ -23,19 +31,6 @@ describe('CommentsComponent', () => {
eventBus: EVENT_BUS_MOCK,
});

const mockFetch = jest.fn();
global.fetch = mockFetch;

mockFetch.mockResolvedValueOnce({
ok: true,
json: jest.fn(),
});

ApiService.fetchAnnotation = jest.fn().mockImplementation(() => []);
ApiService.createAnnotations = jest.fn().mockImplementation(() => []);
ApiService.createComment = jest.fn().mockImplementation(() => []);
ApiService.resolveAnnotation = jest.fn().mockImplementation(() => []);

commentsComponent['element'].addAnnotation = jest.fn().mockImplementation(() => []);
});

Expand All @@ -56,7 +51,7 @@ describe('CommentsComponent', () => {
});

it('should have an element property', () => {
expect(commentsComponent['element']).toBeUndefined();
expect(commentsComponent['element']).toBeDefined();
});

it('should create a new element when start() is called', () => {
Expand All @@ -75,4 +70,58 @@ describe('CommentsComponent', () => {

expect(document.body.contains(commentsComponent['element'])).toBe(false);
});

it('should call apiService when fetch annotation', async () => {
const result = jest.spyOn(ApiService, 'fetchAnnotation');

expect(result).toHaveBeenCalledWith(
expect.any(String),
expect.any(String),
{
roomId: expect.any(String),
url: expect.any(String),
},
);
});

it('should call apiService when create annotation', async () => {
const result = jest.spyOn(ApiService, 'createAnnotations');

commentsComponent['element'].dispatchEvent(new CustomEvent('create-annotation', {
detail: {
text: 'test',
position: {
x: 0,
y: 0,
},
},
}));

expect(result).toHaveBeenCalledWith(
expect.any(String),
expect.any(String),
{
roomId: expect.any(String),
url: expect.any(String),
userId: expect.any(String),
position: expect.any(String),
},
);
});

it('should call apiService when resolve annotation', async () => {
const result = jest.spyOn(ApiService, 'resolveAnnotation');

commentsComponent['element'].dispatchEvent(new CustomEvent('resolve-annotation', {
detail: {
uuid: 'test',
},
}));

expect(result).toHaveBeenCalledWith(
expect.any(String),
expect.any(String),
'test',
);
});
});
37 changes: 21 additions & 16 deletions src/components/comments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,27 @@ export class CommentsComponent extends BaseComponent {
* @returns {Promise<void>}
*/
private createAnnotation = async (e: CustomEvent): Promise<void> => {
const { text, position } = e.detail;
const { url } = this;

const annotation: Annotation = await ApiService.createAnnotations(config.get<string>('apiUrl'), config.get<string>('apiKey'), {
roomId: config.get<string>('roomId'),
position: JSON.stringify(position),
url,
userId: this.localParticipant.id,
});

const comment = await this.createComment(annotation.uuid, text);

this.addAnnotation([{
...annotation,
comments: [comment],
}]);
try {
const { text, position } = e.detail;
const { url } = this;

const annotation: Annotation = await ApiService.createAnnotations(config.get<string>('apiUrl'), config.get<string>('apiKey'), {
roomId: config.get<string>('roomId'),
position: JSON.stringify(position),
url,
userId: this.localParticipant.id,
});

const comment = await this.createComment(annotation.uuid, text);

this.addAnnotation([{
...annotation,
comments: [comment],
}]);
} catch (error) {
this.logger.log('error when creating annotation', error);
throw error;
}
};

/**
Expand Down

0 comments on commit e91bb25

Please sign in to comment.