Skip to content

Commit

Permalink
update LinkService instantiation in AnnotationLayer.spec
Browse files Browse the repository at this point in the history
This is done to match the need to manually set the eventBus on a 
LinkService
  • Loading branch information
kylemellander committed Apr 13, 2020
1 parent cc78bed commit 5c4504d
Showing 1 changed file with 55 additions and 46 deletions.
101 changes: 55 additions & 46 deletions src/Page/AnnotationLayer.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ import LinkService from '../LinkService';

import failingPage from '../../__mocks__/_failing_page';

import {
loadPDF, makeAsyncCallback, muteConsole, restoreConsole,
} from '../../test-utils';
import { loadPDF, makeAsyncCallback, muteConsole, restoreConsole } from '../../test-utils';
import eventBus from '../eventBus';

const pdfFile = loadPDF('./__mocks__/_pdf.pdf');

/* eslint-disable comma-dangle */

describe('AnnotationLayer', () => {
const linkService = new LinkService();
const linkService = new LinkService({ eventBus });

// Loaded page
let page;
Expand All @@ -40,15 +39,16 @@ describe('AnnotationLayer', () => {
describe('loading', () => {
it('loads annotations and calls onGetAnnotationsSuccess callback properly', async () => {
const {
func: onGetAnnotationsSuccess, promise: onGetAnnotationsSuccessPromise
func: onGetAnnotationsSuccess,
promise: onGetAnnotationsSuccessPromise,
} = makeAsyncCallback();

mount(
<AnnotationLayer
linkService={linkService}
onGetAnnotationsSuccess={onGetAnnotationsSuccess}
page={page}
/>
/>,
);

expect.assertions(1);
Expand All @@ -57,7 +57,8 @@ describe('AnnotationLayer', () => {

it('calls onGetAnnotationsError when failed to load annotations', async () => {
const {
func: onGetAnnotationsError, promise: onGetAnnotationsErrorPromise
func: onGetAnnotationsError,
promise: onGetAnnotationsErrorPromise,
} = makeAsyncCallback();

muteConsole();
Expand All @@ -67,7 +68,7 @@ describe('AnnotationLayer', () => {
linkService={linkService}
onGetAnnotationsError={onGetAnnotationsError}
page={failingPage}
/>
/>,
);

expect.assertions(1);
Expand All @@ -78,22 +79,24 @@ describe('AnnotationLayer', () => {

it('replaces annotations properly when page is changed', async () => {
const {
func: onGetAnnotationsSuccess, promise: onGetAnnotationsSuccessPromise
func: onGetAnnotationsSuccess,
promise: onGetAnnotationsSuccessPromise,
} = makeAsyncCallback();

const mountedComponent = mount(
<AnnotationLayer
linkService={linkService}
onGetAnnotationsSuccess={onGetAnnotationsSuccess}
page={page}
/>
/>,
);

expect.assertions(2);
await expect(onGetAnnotationsSuccessPromise).resolves.toMatchObject(desiredAnnotations);

const {
func: onGetAnnotationsSuccess2, promise: onGetAnnotationsSuccessPromise2
func: onGetAnnotationsSuccess2,
promise: onGetAnnotationsSuccessPromise2,
} = makeAsyncCallback();

mountedComponent.setProps({
Expand All @@ -114,15 +117,16 @@ describe('AnnotationLayer', () => {
describe('rendering', () => {
it('renders annotations properly', async () => {
const {
func: onRenderAnnotationLayerSuccess, promise: onRenderAnnotationLayerSuccessPromise,
func: onRenderAnnotationLayerSuccess,
promise: onRenderAnnotationLayerSuccessPromise,
} = makeAsyncCallback();

const component = mount(
<AnnotationLayer
linkService={linkService}
onRenderAnnotationLayerSuccess={onRenderAnnotationLayerSuccess}
page={page}
/>
/>,
);

expect.assertions(1);
Expand All @@ -143,40 +147,44 @@ describe('AnnotationLayer', () => {
${2} | ${'_blank'}
${3} | ${'_parent'}
${4} | ${'_top'}
`('renders all links with target $target given externalLinkTarget = $target',
({ linkServiceTarget, target }) => {
const {
func: onRenderAnnotationLayerSuccess, promise: onRenderAnnotationLayerSuccessPromise,
} = makeAsyncCallback();
const customLinkService = new LinkService();
customLinkService.externalLinkTarget = linkServiceTarget;

const component = mount(
<AnnotationLayer
linkService={customLinkService}
onRenderAnnotationLayerSuccess={onRenderAnnotationLayerSuccess}
page={page}
/>
);

expect.assertions(desiredAnnotations.length);

return onRenderAnnotationLayerSuccessPromise.then(() => {
component.update();
const renderedLayer = component.getDOMNode();
const annotationItems = [...renderedLayer.children];
const annotationLinkItems = annotationItems
.map(item => item.firstChild)
.filter(item => item.tagName === 'A');

annotationLinkItems.forEach(link => expect(link.getAttribute('target')).toBe(target));
});
});
`(
'renders all links with target $target given externalLinkTarget = $target',
({ linkServiceTarget, target }) => {
const {
func: onRenderAnnotationLayerSuccess,
promise: onRenderAnnotationLayerSuccessPromise,
} = makeAsyncCallback();
const customLinkService = new LinkService();
customLinkService.externalLinkTarget = linkServiceTarget;

const component = mount(
<AnnotationLayer
linkService={customLinkService}
onRenderAnnotationLayerSuccess={onRenderAnnotationLayerSuccess}
page={page}
/>,
);

expect.assertions(desiredAnnotations.length);

return onRenderAnnotationLayerSuccessPromise.then(() => {
component.update();
const renderedLayer = component.getDOMNode();
const annotationItems = [...renderedLayer.children];
const annotationLinkItems = annotationItems
.map(item => item.firstChild)
.filter(item => item.tagName === 'A');

annotationLinkItems.forEach(link => expect(link.getAttribute('target')).toBe(target));
});
},
);
/* eslint-enable indent */

it('renders annotations at a given rotation', async () => {
const {
func: onRenderAnnotationLayerSuccess, promise: onRenderAnnotationLayerSuccessPromise,
func: onRenderAnnotationLayerSuccess,
promise: onRenderAnnotationLayerSuccessPromise,
} = makeAsyncCallback();
const rotate = 90;

Expand All @@ -186,7 +194,7 @@ describe('AnnotationLayer', () => {
onRenderAnnotationLayerSuccess={onRenderAnnotationLayerSuccess}
page={page}
rotate={rotate}
/>
/>,
);

expect.assertions(1);
Expand All @@ -200,7 +208,8 @@ describe('AnnotationLayer', () => {

it('renders annotations at a given scale', async () => {
const {
func: onRenderAnnotationLayerSuccess, promise: onRenderAnnotationLayerSuccessPromise,
func: onRenderAnnotationLayerSuccess,
promise: onRenderAnnotationLayerSuccessPromise,
} = makeAsyncCallback();
const scale = 2;

Expand All @@ -210,7 +219,7 @@ describe('AnnotationLayer', () => {
onRenderAnnotationLayerSuccess={onRenderAnnotationLayerSuccess}
page={page}
scale={scale}
/>
/>,
);

expect.assertions(1);
Expand Down

0 comments on commit 5c4504d

Please sign in to comment.