From 56e44494952e43d124705bc2386637d931d97b1f Mon Sep 17 00:00:00 2001 From: anastasiia Date: Tue, 27 Feb 2024 07:53:32 -0600 Subject: [PATCH 1/5] add unit tests --- client/jest.config.js | 2 +- client/src/utils/commandBarUtils.ts | 2 +- client/src/utils/index.test.ts | 69 ++++++++++++++++++++++++++++- 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/client/jest.config.js b/client/jest.config.js index 3089a1234c..f5b4755c8b 100644 --- a/client/jest.config.js +++ b/client/jest.config.js @@ -4,7 +4,7 @@ module.exports = { testEnvironment: 'jsdom', setupFilesAfterEnv: ['/tests/setupTests.ts'], collectCoverageFrom: [ - 'src/**/*.{js,jsx,ts,tsx}', + 'src/utils/*.{js,jsx,ts,tsx}', '!**/node_modules/**', '!**/vendor/**', ], diff --git a/client/src/utils/commandBarUtils.ts b/client/src/utils/commandBarUtils.ts index 356fc6f045..0358a6d3ce 100644 --- a/client/src/utils/commandBarUtils.ts +++ b/client/src/utils/commandBarUtils.ts @@ -21,7 +21,7 @@ export const bubbleUpRecentItems = ( newSections.push({ label: recentLabel, items: recentItems.sort( - (a, b) => recentKeys.indexOf(b.key) - recentKeys.indexOf(a.key), + (a, b) => recentKeys.indexOf(a.key) - recentKeys.indexOf(b.key), ), key: 'recent-items', }); diff --git a/client/src/utils/index.test.ts b/client/src/utils/index.test.ts index 6f2b16b314..79cb6c2446 100644 --- a/client/src/utils/index.test.ts +++ b/client/src/utils/index.test.ts @@ -1,5 +1,9 @@ import { ParsedQueryTypeEnum } from '../types/general'; -import { splitUserInputAfterAutocomplete } from './index'; +import { + getCommonFolder, + getFileExtensionForLang, + splitUserInputAfterAutocomplete, +} from './index'; describe('Utils', () => { describe('splitUserInputAfterAutocomplete', () => { @@ -99,4 +103,67 @@ describe('Utils', () => { ); }); }); + 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('/'); + }); + }); }); From 6476331f695f165e35f8e8cd038e07e1ca6e4b0e Mon Sep 17 00:00:00 2001 From: anastasiia Date: Tue, 27 Feb 2024 07:53:54 -0600 Subject: [PATCH 2/5] add tests to gh workflow --- .github/workflows/client-test.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/client-test.yml b/.github/workflows/client-test.yml index a6ecb4692b..62029ecb90 100644 --- a/.github/workflows/client-test.yml +++ b/.github/workflows/client-test.yml @@ -44,3 +44,6 @@ jobs: - name: Run type-check run: npm run client-type-check + + - name: Run tests + run: npm --prefix client run test From 647e4918dfe3839497d9f6b9f46e2cc99a59e725 Mon Sep 17 00:00:00 2001 From: anastasiia Date: Tue, 27 Feb 2024 07:59:42 -0600 Subject: [PATCH 3/5] lower coverage requirements --- client/jest.config.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/client/jest.config.js b/client/jest.config.js index f5b4755c8b..ee0ef33270 100644 --- a/client/jest.config.js +++ b/client/jest.config.js @@ -4,16 +4,15 @@ module.exports = { testEnvironment: 'jsdom', setupFilesAfterEnv: ['/tests/setupTests.ts'], collectCoverageFrom: [ - 'src/utils/*.{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: 6, + functions: 16, + lines: 13, }, }, }; From 8085a4ec2e5a64ed884f68611651f86614c99049 Mon Sep 17 00:00:00 2001 From: anastasiia Date: Tue, 27 Feb 2024 08:02:28 -0600 Subject: [PATCH 4/5] test fix --- client/src/utils/commandBarUtils.test.ts | 4 ++-- client/src/utils/commandBarUtils.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/client/src/utils/commandBarUtils.test.ts b/client/src/utils/commandBarUtils.test.ts index ffe2996039..7b8b9e86d4 100644 --- a/client/src/utils/commandBarUtils.test.ts +++ b/client/src/utils/commandBarUtils.test.ts @@ -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); @@ -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); diff --git a/client/src/utils/commandBarUtils.ts b/client/src/utils/commandBarUtils.ts index 0358a6d3ce..356fc6f045 100644 --- a/client/src/utils/commandBarUtils.ts +++ b/client/src/utils/commandBarUtils.ts @@ -21,7 +21,7 @@ export const bubbleUpRecentItems = ( newSections.push({ label: recentLabel, items: recentItems.sort( - (a, b) => recentKeys.indexOf(a.key) - recentKeys.indexOf(b.key), + (a, b) => recentKeys.indexOf(b.key) - recentKeys.indexOf(a.key), ), key: 'recent-items', }); From 7c920d3d972e4a0d8181730a46bae3e7dd1a63ce Mon Sep 17 00:00:00 2001 From: anastasiia Date: Tue, 27 Feb 2024 09:42:07 -0600 Subject: [PATCH 5/5] add more tests --- client/jest.config.js | 6 +- client/src/utils/index.test.ts | 132 +++++++++++++++++++++++++++++++++ client/src/utils/index.ts | 18 ----- 3 files changed, 135 insertions(+), 21 deletions(-) diff --git a/client/jest.config.js b/client/jest.config.js index ee0ef33270..ef901d3aef 100644 --- a/client/jest.config.js +++ b/client/jest.config.js @@ -10,9 +10,9 @@ module.exports = { ], coverageThreshold: { global: { - branches: 6, - functions: 16, - lines: 13, + branches: 10, + functions: 21, + lines: 18, }, }, }; diff --git a/client/src/utils/index.test.ts b/client/src/utils/index.test.ts index 79cb6c2446..1ec81b2c7e 100644 --- a/client/src/utils/index.test.ts +++ b/client/src/utils/index.test.ts @@ -1,7 +1,10 @@ import { ParsedQueryTypeEnum } from '../types/general'; import { + concatenateParsedQuery, getCommonFolder, getFileExtensionForLang, + humanNumber, + mergeRanges, splitUserInputAfterAutocomplete, } from './index'; @@ -102,6 +105,23 @@ 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', () => { @@ -166,4 +186,116 @@ describe('Utils', () => { 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); + }); + }); }); diff --git a/client/src/utils/index.ts b/client/src/utils/index.ts index e5214f8452..4f0934f67f 100644 --- a/client/src/utils/index.ts +++ b/client/src/utils/index.ts @@ -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 = () => {};