Skip to content

Commit

Permalink
Restore JS tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fcollonval committed Mar 13, 2021
1 parent c081dbd commit e50ae3f
Show file tree
Hide file tree
Showing 17 changed files with 451 additions and 273 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ node_modules
dist
coverage
**/*.d.ts
tests
lib
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
],
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
project: 'tsconfig.eslint.json',
sourceType: 'module'
},
plugins: ['@typescript-eslint'],
Expand Down
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ const {
module.exports = {
coverageDirectory,
moduleFileExtensions,
moduleNameMapper: {...moduleNameMapper, "monaco-editor": "<rootDir>/node_modules/react-monaco-editor"},
moduleNameMapper,
preset,
setupFilesAfterEnv,
setupFiles,
testPathIgnorePatterns,
transform,
automock: false,
testRegex: 'src/tests/.*.spec.ts[x]?$',
transformIgnorePatterns: ['/node_modules/(?!(@?jupyterlab.*)/)'],
transformIgnorePatterns: ['/node_modules/(?!(@?jupyterlab.*|react-spinners)/)'],
setupFiles: ['<rootDir>/setupJest.js'],
globals: {
'ts-jest': {
Expand Down
12 changes: 6 additions & 6 deletions src/components/browser/PullRequestItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
linkIcon
} from '@jupyterlab/ui-components';
import { CommandRegistry } from '@lumino/commands';
import React, { useEffect, useState } from 'react';
import React from 'react';
import { BeatLoader } from 'react-spinners';
import { CommandIDs, IFile, IPullRequest } from '../../tokens';
import { requestAPI } from '../../utils';
Expand Down Expand Up @@ -49,21 +49,21 @@ export function PullRequestItem(props: IPullRequestItemProps): JSX.Element {
/**
* Pull request modified files
*/
const [files, setFiles] = useState<IFile[] | null>(null);
const [files, setFiles] = React.useState<IFile[] | null>(null);
/**
* Is the file list expanded?
*/
const [isExpanded, setIsExpanded] = useState<boolean>(false);
const [isExpanded, setIsExpanded] = React.useState<boolean>(false);
/**
* Is the file list being loaded?
*/
const [isLoading, setIsLoading] = useState<boolean>(false);
const [isLoading, setIsLoading] = React.useState<boolean>(false);
/**
* Error message
*/
const [error, setError] = useState<string | null>(null);
const [error, setError] = React.useState<string | null>(null);

useEffect(() => {
React.useEffect(() => {
setFiles(null);
setIsExpanded(false);
setIsLoading(false);
Expand Down
30 changes: 17 additions & 13 deletions src/components/discussion/Discussion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ export class Discussion extends Panel {
: 'Leave a comment';

const node = generateNode('div', {
class: 'jp-PullRequestCommentItem'
class: 'jp-PullRequestCommentItem',
});
const p = node
.appendChild(
generateNode('div', { class: 'jp-PullRequestCommentItemContent' })
)
.appendChild(
generateNode('p', {
class: 'jp-PullRequestCommentItemContentTitle'
class: 'jp-PullRequestCommentItemContentTitle',
})
);
p.innerHTML = msg;
Expand All @@ -94,9 +94,11 @@ export class Discussion extends Panel {
latestWidget.parent = null;
latestWidget.dispose();

this.addWidget(
this._inputShown ? this.createCommentInput() : this.createReplyButton()
);
if (this._inputShown) {
this.addWidget(this.createCommentInput());
} else if (this._thread.singleton !== true) {
this.addWidget(this.createReplyButton());
}
}
}

Expand All @@ -105,7 +107,7 @@ export class Discussion extends Panel {
*/
protected initNode(): void {
const expandButton = generateNode('button', {
class: 'jp-PullRequestExpandButton'
class: 'jp-PullRequestExpandButton',
}) as HTMLButtonElement;
expandButton.appendChild(
caretUpIcon.element({ tag: 'span', title: 'Collapse Discussion' })
Expand Down Expand Up @@ -133,15 +135,17 @@ export class Discussion extends Panel {
* Add the thread view in the widget
*/
protected addThreadView(): void {
this._thread.comments.forEach(comment => {
this._thread.comments.forEach((comment) => {
this.addWidget(
new CommentWidget({ comment, renderMime: this._renderMime })
);
});
if (this._inputShown) {
this.addWidget(this.createCommentInput());
} else {
this.addWidget(this.createReplyButton());
if (this._thread.singleton !== true) {
this.addWidget(this.createReplyButton());
}
}
}

Expand All @@ -156,7 +160,7 @@ export class Discussion extends Panel {
body = {
...body,
line: this._thread.line,
originalLine: this._thread.originalLine
originalLine: this._thread.originalLine,
};
} else {
body = { ...body, discussionId: this._thread.id };
Expand All @@ -176,7 +180,7 @@ export class Discussion extends Panel {
text: response.text,
updatedAt: response.updateAt,
userName: response.userName,
userPicture: response.userPicture
userPicture: response.userPicture,
};
// Update discussion reference
if (response.inReplyTo) {
Expand Down Expand Up @@ -215,7 +219,7 @@ export class Discussion extends Panel {
private createCommentInput(): InputComment {
const widget = new InputComment({
handleSubmit: this.handleAddComment.bind(this),
handleCancel: this.handleCancelComment.bind(this)
handleCancel: this.handleCancelComment.bind(this),
});
widget.addClass('jp-PullRequestCommentItem');
return widget;
Expand All @@ -235,12 +239,12 @@ export class Discussion extends Panel {
{
click: () => {
this.inputShown = true;
}
},
}
)
);
return new Widget({
node
node,
});
}

Expand Down
32 changes: 32 additions & 0 deletions src/tests/Browser.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { shallow } from 'enzyme';
import 'jest';
import * as React from 'react';
import * as samplePRs from './sample-responses/samplepr.json';
import { IBrowserProps, Browser } from '../components/browser/Browser';

// Unit tests for PullRequestTab
describe('Browser', () => {
const DEFAULT_PROPS: IBrowserProps = {
commands: {} as any,
docRegistry: {} as any,
prGroups: [
{ name: 'group1', pullRequests: (samplePRs as any).default },
{ name: 'group2', pullRequests: (samplePRs as any).default }
]
};

// Test render
describe('#render()', () => {
const component = shallow(<Browser {...DEFAULT_PROPS} />);
it('should be a div', () => {
expect(component.find('div')).toHaveLength(1);
expect(component.find('.jp-PullRequestBrowser')).toHaveLength(1);
});
it('should have a list', () => {
expect(component.find('ul')).toHaveLength(1);
});
it('should have two BrowserGroup', () => {
expect(component.find('BrowserGroup')).toHaveLength(2);
});
});
});
84 changes: 84 additions & 0 deletions src/tests/BrowserGroup.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { shallow } from 'enzyme';
import 'jest';
import * as React from 'react';
import {
BrowserGroup,
IBrowserGroupProps,
} from '../components/browser/BrowserGroup';
import { PullRequestItem } from '../components/browser/PullRequestItem';
import * as samplePRs from './sample-responses/samplepr.json';

// Unit tests for BrowserGroup
describe('BrowserGroup', () => {
const DEFAULT_PROPS: IBrowserGroupProps = {
commands: {} as any,
docRegistry: {} as any,
group: { name: 'group1', pullRequests: (samplePRs as any).default },
};

// Test render
describe('#render()', () => {
it('should be a list item', () => {
const component = shallow(<BrowserGroup {...DEFAULT_PROPS} />);
expect(component.find('li')).toHaveLength(1);
expect(component.find('.jp-PullRequestBrowserGroup')).toHaveLength(1);
});

it('should have a header with text props.header', () => {
const component = shallow(<BrowserGroup {...DEFAULT_PROPS} />);
expect(component.find('header h2')).toHaveLength(1);
expect(component.contains([<h2>{DEFAULT_PROPS.group.name}</h2>])).toEqual(
true
);
});

it('should load group', () => {
const component = shallow(<BrowserGroup {...DEFAULT_PROPS} />);

expect(component.find('.jp-PullRequestBrowserGroupError')).toHaveLength(
0
);
expect(component.find('ul')).toHaveLength(1);
expect(component.find('.jp-PullRequestBrowserGroupList')).toHaveLength(1);
});

it('should load list item prs', () => {
const component = shallow(<BrowserGroup {...DEFAULT_PROPS} />);
expect(component.find(PullRequestItem)).toHaveLength(1);
});

it('should not have a group if there is an error', () => {
const component = shallow(
<BrowserGroup
{...DEFAULT_PROPS}
group={{
name: 'error',
pullRequests: [],
error: 'There is an error',
}}
/>
);
expect(component.find('ul')).toHaveLength(0);
expect(component.find('.jp-PullRequestBrowserGroupList')).toHaveLength(0);
});

it('should display error if one exists', () => {
const error = 'There is an error';
const component = shallow(
<BrowserGroup
{...DEFAULT_PROPS}
group={{
name: 'error',
pullRequests: [],
error,
}}
/>
);

expect(component.find('.jp-PullRequestBrowserGroupError')).toHaveLength(
1
);
expect(component.contains(error)).toEqual(true);
});
});
});
Loading

0 comments on commit e50ae3f

Please sign in to comment.