Skip to content

Commit

Permalink
Add unit tests to FE (#1241)
Browse files Browse the repository at this point in the history
* add unit tests

* add tests to gh workflow

* lower coverage requirements

* test fix

* add more tests
  • Loading branch information
anastasiya1155 authored Apr 5, 2024
1 parent 87da96f commit 77a7ddb
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 26 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/client-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ jobs:

- name: Run type-check
run: npm run client-type-check

- name: Run tests
run: npm --prefix client run test
9 changes: 4 additions & 5 deletions client/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ module.exports = {
testEnvironment: 'jsdom',
setupFilesAfterEnv: ['<rootDir>/tests/setupTests.ts'],
collectCoverageFrom: [
'src/**/*.{js,jsx,ts,tsx}',
'src/utils/{!(services),}.{js,jsx,ts,tsx}',
'!**/node_modules/**',
'!**/vendor/**',
],
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: -10,
branches: 10,
functions: 21,
lines: 18,
},
},
};
4 changes: 2 additions & 2 deletions client/src/utils/commandBarUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ describe('commandBarUtils', () => {
const key1 = items1[2].key;
const key2 = items2[1].key;
const key3 = items3[0].key;
const recentKeysArr = [key1, key2, key3];
const recentKeysArr = [key3, key2, key1];
const result = bubbleUpRecentItems(sections, recentKeysArr, recentLabel);
expect(result.length).toEqual(3);
expect(result[0].items.length).toEqual(recentKeysArr.length);
Expand All @@ -174,7 +174,7 @@ describe('commandBarUtils', () => {
...items1.map((i) => i.key),
...items2.map((i) => i.key),
...items3.map((i) => i.key),
];
].reverse();
const result = bubbleUpRecentItems(sections, recentKeysArr, recentLabel);
expect(result.length).toEqual(1);
expect(result[0].items.length).toEqual(recentKeysArr.length);
Expand Down
201 changes: 200 additions & 1 deletion client/src/utils/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { ParsedQueryTypeEnum } from '../types/general';
import { splitUserInputAfterAutocomplete } from './index';
import {
concatenateParsedQuery,
getCommonFolder,
getFileExtensionForLang,
humanNumber,
mergeRanges,
splitUserInputAfterAutocomplete,
} from './index';

describe('Utils', () => {
describe('splitUserInputAfterAutocomplete', () => {
Expand Down Expand Up @@ -98,5 +105,197 @@ describe('Utils', () => {
]),
);
});
test('repo filter after lang filter in the middle', () => {
expect(
JSON.stringify(
splitUserInputAfterAutocomplete(
'my |lang:TypeScript| simple |repo:BloopAI/bloop| string',
),
),
).toEqual(
JSON.stringify([
{ type: ParsedQueryTypeEnum.TEXT, text: 'my ' },
{ type: ParsedQueryTypeEnum.LANG, text: 'TypeScript' },
{ type: ParsedQueryTypeEnum.TEXT, text: ' simple ' },
{ type: ParsedQueryTypeEnum.REPO, text: 'BloopAI/bloop' },
{ type: ParsedQueryTypeEnum.TEXT, text: ' string' },
]),
);
});
});
describe('getFileExtensionForLang', () => {
test('main languages', () => {
expect(getFileExtensionForLang('JavaScript')).toEqual('index.js');
expect(getFileExtensionForLang('TypeScript')).toEqual('index.ts');
expect(getFileExtensionForLang('JSX')).toEqual('index.jsx');
expect(getFileExtensionForLang('Rust')).toEqual('index.rs');
});
test('lowercased', () => {
expect(getFileExtensionForLang('javascript', true)).toEqual('index.js');
expect(getFileExtensionForLang('typescript', true)).toEqual('index.ts');
expect(getFileExtensionForLang('jsx', true)).toEqual('index.jsx');
expect(getFileExtensionForLang('rust', true)).toEqual('index.rs');
});
test('unknown languages', () => {
expect(getFileExtensionForLang('asd', true)).toEqual('index.asd');
expect(getFileExtensionForLang('ASD')).toEqual('index.ASD');
expect(getFileExtensionForLang('Asd')).toEqual('index.Asd');
});
test('empty input', () => {
expect(getFileExtensionForLang('', true)).toEqual('default');
expect(getFileExtensionForLang('')).toEqual('default');
});
});
describe('getCommonFolder', () => {
test('no folder', () => {
expect(getCommonFolder(['index.js', '.gitignore'])).toEqual('');
});
test('no common folder', () => {
expect(getCommonFolder(['src/index.js', 'public/.gitignore'])).toEqual(
'',
);
expect(getCommonFolder(['/src/index.js', '/public/.gitignore'])).toEqual(
'',
);
expect(
getCommonFolder(['src/index.js', 'public/src/.gitignore']),
).toEqual('');
});
test('one common folder', () => {
expect(
getCommonFolder(['src/components/index.js', 'src/utils.js']),
).toEqual('src');
expect(
getCommonFolder(['src/components/index.js', 'src/utils/utils.js']),
).toEqual('src');
});
test('two common folders', () => {
expect(
getCommonFolder(['src/components/index.js', 'src/components/utils.js']),
).toEqual('src/components');
});
test('windows path', () => {
expect(
getCommonFolder([
'\\src\\components\\index.js',
'\\src\\components\\utils.js',
]),
).toEqual('\\src\\components');
});
test('empty input', () => {
expect(getCommonFolder([])).toEqual('/');
});
});
describe('concatenateParsedQuery', () => {
test('no filters used', () => {
expect(
concatenateParsedQuery([
{ type: ParsedQueryTypeEnum.TEXT, text: 'Hello world!' },
]),
).toEqual('Hello world!');
expect(
concatenateParsedQuery([
{ type: ParsedQueryTypeEnum.TEXT, text: 'Hello' },
{ type: ParsedQueryTypeEnum.TEXT, text: ' world!' },
]),
).toEqual('Hello world!');
});
test('filters used', () => {
expect(
concatenateParsedQuery([
{ type: ParsedQueryTypeEnum.TEXT, text: 'Hello ' },
{ type: ParsedQueryTypeEnum.LANG, text: 'js' },
{ type: ParsedQueryTypeEnum.TEXT, text: ' world ' },
{ type: ParsedQueryTypeEnum.REPO, text: 'BloopAI/bloop' },
{ type: ParsedQueryTypeEnum.TEXT, text: ' ' },
{ type: ParsedQueryTypeEnum.PATH, text: 'src/index.js' },
{ type: ParsedQueryTypeEnum.TEXT, text: ' ? ' },
{ type: ParsedQueryTypeEnum.BRANCH, text: 'origin/main' },
{ type: ParsedQueryTypeEnum.PATH, text: 'src/components/index.js' },
]),
).toEqual(
'Hello |lang:js| world |repo:BloopAI/bloop| |path:src/index.js| ? |path:src/components/index.js|',
);
});
});
describe('mergeRanges', () => {
test('empty ranges', () => {
expect(mergeRanges([])).toEqual([]);
});
test('no overlap ranges', () => {
expect(
mergeRanges([
[1, 5],
[7, 10],
]),
).toEqual([
[1, 5],
[7, 10],
]);
expect(
mergeRanges([
[7, 10],
[1, 5],
]),
).toEqual([
[1, 5],
[7, 10],
]);
});
test('no overlap ranges next to each other', () => {
expect(
mergeRanges([
[1, 5],
[6, 10],
]),
).toEqual([[1, 10]]);
expect(
mergeRanges([
[7, 10],
[1, 5],
[11, 15],
]),
).toEqual([
[1, 5],
[7, 15],
]);
});
test('overlap ranges', () => {
expect(
mergeRanges([
[1, 5],
[3, 10],
]),
).toEqual([[1, 10]]);
expect(
mergeRanges([
[7, 10],
[1, 5],
[9, 15],
]),
).toEqual([
[1, 5],
[7, 15],
]);
});
});
describe('humanNumber', () => {
test('< 1000', () => {
expect(humanNumber(123)).toEqual('123');
expect(humanNumber(999)).toEqual('999');
expect(humanNumber(5)).toEqual('5');
});
test('> 1000', () => {
expect(humanNumber(1000)).toEqual('1k');
expect(humanNumber(1001)).toEqual('1k');
expect(humanNumber(5432)).toEqual('5.4k');
expect(humanNumber(5999)).toEqual('6k');
expect(humanNumber(10009)).toEqual('10k');
expect(humanNumber(100009)).toEqual('100k');
expect(humanNumber(1000009)).toEqual('1000k');
});
test('none', () => {
expect(humanNumber(0)).toEqual(0);
});
});
});
18 changes: 0 additions & 18 deletions client/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,22 +470,4 @@ export function concatenateParsedQuery(query: ParsedQueryType[]) {
return result;
}

type InputEditorTextContent = {
type: 'text';
text: string;
};

type InputEditorMentionContent = {
type: 'mention';
attrs: {
type: 'lang' | 'path' | 'repo';
id: string;
display: string;
};
};

export type InputEditorContent =
| InputEditorTextContent
| InputEditorMentionContent;

export const noOp = () => {};

0 comments on commit 77a7ddb

Please sign in to comment.