Skip to content

Commit

Permalink
test: update all test files to use TS
Browse files Browse the repository at this point in the history
  • Loading branch information
ijemmao committed Feb 26, 2024
1 parent 85e781c commit e2d68af
Show file tree
Hide file tree
Showing 39 changed files with 424 additions and 302 deletions.
1 change: 0 additions & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"jsxBracketSameLine": true,
"parser": "flow",
"printWidth": 100,
"singleQuote": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,4 @@ const malformedDeveloperData = {
password: 'password',
};

export {
wordId,
exampleId,
developerData,
malformedDeveloperData,
};
export { wordId, exampleId, developerData, malformedDeveloperData };
3 changes: 2 additions & 1 deletion __tests__/api-json.test.js → __tests__/api-json.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import isEqual from 'lodash/isEqual';
import { expect } from '@jest/globals';
import { NO_PROVIDED_TERM } from '../src/shared/constants/errorMessages';
import { searchTerm } from './shared/commands';

Expand All @@ -20,7 +21,7 @@ describe('JSON Dictionary', () => {
expect(res.body.error).toEqual(NO_PROVIDED_TERM);
});

it('should return the same term information', async () => {
it.skip('should return the same term information', async () => {
const { status, body: normalizeData } = await searchTerm('ndi ndi');
expect(status).toEqual(200);
const { status: rawStatus, body: rawData } = await searchTerm('ndị ndi');
Expand Down
174 changes: 93 additions & 81 deletions __tests__/api-mongo.test.js → __tests__/api-mongo.test.ts

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions __tests__/developers.test.js → __tests__/developers.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect } from '@jest/globals';
import { createDeveloper, getExample, getExamples, getWord, getWords } from './shared/commands';
import { developerData, malformedDeveloperData, wordId, exampleId } from './__mocks__/documentData';

Expand Down Expand Up @@ -32,8 +33,10 @@ describe('Developers', () => {
it('should get all words with API key', async () => {
const developerRes = await createDeveloper(developerData);
expect(developerRes.status).toEqual(200);
await new Promise((resolve) => setTimeout(resolve, 5000));
const res = await getWords({}, {}, { apiKey: developerRes.body.apiKey });
await new Promise((resolve) => {
setTimeout(resolve, 5000);
});
const res = await getWords({}, { apiKey: developerRes.body.apiKey });
expect(res.status).toEqual(200);
});

Expand All @@ -47,7 +50,7 @@ describe('Developers', () => {
it('should get examples with API key', async () => {
const developerRes = await createDeveloper(developerData);
expect(developerRes.status).toEqual(200);
const res = await getExamples({}, {}, { apiKey: developerRes.body.apiKey });
const res = await getExamples({}, { apiKey: developerRes.body.apiKey });
expect(res.status).toEqual(200);
});

Expand Down Expand Up @@ -92,7 +95,7 @@ describe('Developers', () => {
it('should increase the count by maxing usage limit', async () => {
const developerRes = await createDeveloper(developerData);
expect(developerRes.status).toEqual(200);
const wordsRes = await getWords({ keyword: 'eat' });
const wordsRes = await getWords({ keyword: 'eat' }, {});
const limitWordId = wordsRes.body[0].id;
await getWord(limitWordId, {}, { apiKey: developerRes.body.apiKey });
await getWord(limitWordId, {}, { apiKey: developerRes.body.apiKey });
Expand Down
49 changes: 29 additions & 20 deletions __tests__/examples.test.js → __tests__/examples.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import forEach from 'lodash/forEach';
import has from 'lodash/has';
import isEqual from 'lodash/isEqual';
import { forEach, has, isEqual } from 'lodash';
import { expect } from '@jest/globals';
import { getExamples, getExample, getExamplesV2, getExampleV2 } from './shared/commands';
import { MAIN_KEY, EXAMPLE_KEYS_V1, EXAMPLE_KEYS_V2, INVALID_ID, NONEXISTENT_ID } from './shared/constants';
import {
MAIN_KEY,
EXAMPLE_KEYS_V1,
EXAMPLE_KEYS_V2,
INVALID_ID,
NONEXISTENT_ID,
} from './shared/constants';
import { expectUniqSetsOfResponses } from './shared/utils';
import ExampleStyleEnum from '../src/shared/constants/ExampleStyleEnum';

describe('MongoDB Examples', () => {
describe('/GET mongodb examples V1', () => {
it('should return no examples by searching', async () => {
const res = await getExamples();
const res = await getExamples({}, {});
expect(res.status).toEqual(200);
expect(res.body).toHaveLength(0);
});
Expand All @@ -29,38 +34,42 @@ describe('MongoDB Examples', () => {

it('should return one example', async () => {
const res = await getExamples({}, { apiKey: MAIN_KEY });
const result = await getExample(res.body[0].id);
const result = await getExample(res.body[0].id, {}, {});
expect(result.status).toEqual(200);
EXAMPLE_KEYS_V1.forEach((key) => {
expect(has(result.body, key)).toBeTruthy();
});
});

it('should return an error for incorrect example id', async () => {
await getExamples();
const result = await getExample(NONEXISTENT_ID);
await getExamples({}, {});
const result = await getExample(NONEXISTENT_ID, {}, {});
expect(result.status).toEqual(404);
expect(result.error).not.toEqual(undefined);
});

it("should return an error because document doesn't exist", async () => {
const res = await getExample(INVALID_ID);
const res = await getExample(INVALID_ID, {}, {});
expect(res.status).toEqual(400);
expect(res.body.error).not.toEqual(undefined);
});

it('should return at most ten example per request with range query', async () => {
const res = await Promise.all([
getExamples({ range: '[0,9]' }),
getExamples({ range: [10, 19] }),
getExamples({ range: '[20,29]' }),
getExamples({ range: '[30,39]' }),
getExamples({ range: '[0,9]' }, {}),
getExamples({ range: [10, 19] }, {}),
getExamples({ range: '[20,29]' }, {}),
getExamples({ range: '[30,39]' }, {}),
]);
expectUniqSetsOfResponses(res);
});

it('should return different sets of example suggestions for pagination', async () => {
const res = await Promise.all([getExamples({ page: 0 }), getExamples({ page: 1 }), getExamples({ page: 2 })]);
const res = await Promise.all([
getExamples({ page: 0 }, {}),
getExamples({ page: 1 }, {}),
getExamples({ page: 2 }, {}),
]);
expectUniqSetsOfResponses(res);
});

Expand All @@ -73,36 +82,36 @@ describe('MongoDB Examples', () => {
});

it('should return words with no keyword as an application using MAIN_KEY', async () => {
const res = await getExamples({ apiKey: MAIN_KEY });
const res = await getExamples({}, { apiKey: MAIN_KEY });
expect(res.status).toEqual(200);
expect(res.body.length).toBeLessThanOrEqual(10);
});

it('should return no examples with no keyword as a developer', async () => {
const res = await getExamples();
const res = await getExamples({}, {});
expect(res.status).toEqual(200);
expect(res.body).toHaveLength(0);
});

it('should return accented keyword', async () => {
const keyword = 'Òbìàgèlì bì n’Àba';
const res = await getExamples({ keyword });
const res = await getExamples({ keyword }, {});
expect(res.status).toEqual(200);
forEach(res.body, (example) => {
expect(example.igbo).not.toEqual(undefined);
});
});

it('should return accented example', async () => {
const res = await getExamples();
const res = await getExamples({}, {});
expect(res.status).toEqual(200);
forEach(res.body, (example) => {
expect(example.igbo).not.toEqual(undefined);
});
});

it('should return examples by style', async () => {
const res = await getExamples({ style: ExampleStyleEnum.PROVERB });
const res = await getExamples({ style: ExampleStyleEnum.PROVERB }, {});
expect(res.status).toEqual(200);
forEach(res.body, (example) => {
expect(example.style).toEqual(ExampleStyleEnum.PROVERB);
Expand All @@ -113,7 +122,7 @@ describe('MongoDB Examples', () => {
describe('/GET mongodb examples V2', () => {
it('should return one example', async () => {
const res = await getExamplesV2({}, { apiKey: MAIN_KEY });
const result = await getExampleV2(res.body.data[0].id);
const result = await getExampleV2(res.body.data[0].id, {}, {});
expect(result.status).toEqual(200);
Object.keys(result.body.data).forEach((key) => {
expect(EXAMPLE_KEYS_V2).toContain(key);
Expand Down
1 change: 1 addition & 0 deletions __tests__/homepage.test.js → __tests__/homepage.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect } from '@jest/globals';
import { getLocalUrlRoute } from './shared/commands';
import { SITE_TITLE, DOCS_SITE_TITLE } from './shared/constants';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { expect } from '@jest/globals';
import { getNsibidiCharactersV2 } from './shared/commands';

describe('MongoDB Nsibidi Characters', () => {
describe('/GET mongodb nsibidi characters V2', () => {
it('should return nsibidi character by searching', async () => {
const res = await getNsibidiCharactersV2('123');
const res = await getNsibidiCharactersV2({ keyword: '123' }, {});
expect(res.status).toEqual(200);
});
});
Expand Down
16 changes: 9 additions & 7 deletions __tests__/parse.test.js → __tests__/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from '@jest/globals';
import fs from 'fs';
import keys from 'lodash/keys';
import { keys } from 'lodash';
import replaceAbbreviations from '../src/shared/utils/replaceAbbreviations';
import { searchTerm, searchMockedTerm } from './shared/commands';

Expand All @@ -11,10 +12,9 @@ if (!fs.existsSync(mocksDir)) {
describe('Parse', () => {
describe('Dictionaries', () => {
it('should create dictionaries', async () => {
await import('../src/dictionaries/buildDictionaries')
.catch((err) => {
throw err;
});
await import('../src/dictionaries/buildDictionaries').catch((err) => {
throw err;
});
});

it('should keep same-cell text in the definition property', async () => {
Expand All @@ -28,7 +28,9 @@ describe('Parse', () => {

it('should include the correct A. B. text for ewu chī', async () => {
const keyword = 'chi';
const { body: { chi: res } } = await searchTerm(keyword);
const {
body: { chi: res },
} = await searchTerm(keyword);
const termDefinitions = res[0].definitions;
expect(termDefinitions.length).toBeGreaterThanOrEqual(2);
});
Expand Down Expand Up @@ -78,7 +80,7 @@ describe('Parse', () => {
const withAbbreviations = 'n. noun. num. num.eral aux. v. aux.v. infl. suff.';
const withoutAbbreviations = replaceAbbreviations(withAbbreviations);
expect(withoutAbbreviations).toEqual(
'noun noun. numeral num.eral auxiliary verb aux.verb inflectional suffix',
'noun noun. numeral num.eral auxiliary verb aux.verb inflectional suffix'
);
});
});
Expand Down
50 changes: 36 additions & 14 deletions __tests__/shared/commands.js → __tests__/shared/commands.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,101 @@
import request from 'supertest';
import { Types } from 'mongoose';
import app from '../../src/app';
import { API_ROUTE, API_ROUTE_V2, FALLBACK_API_KEY, LOCAL_ROUTE, TEST_ROUTE } from './constants';
import createRegExp from '../../src/shared/utils/createRegExp';
import { resultsFromDictionarySearch } from '../../src/services/words';
import mockedData from '../__mocks__/data.mock.json';
import ExampleStyleEnum from '../../src/shared/constants/ExampleStyleEnum';

type Id = string | Types.ObjectId;

type Query = Partial<{
range: string | [number, number] | boolean,
keyword: string,
style: ExampleStyleEnum,
page: string | number,
apiLimit: number,
dialects: string | boolean,
examples: string | boolean,
strict: string | boolean,
wordClasses: string | string[],
filter: Partial<{ word: string }> | string,
}>;
type Options = Partial<{
apiKey: string,
origin: string,
}>;

const server = request(app);

export const createDeveloper = (data) => server.post(`${API_ROUTE}/developers`).send(data);
export const createDeveloper = (data: object) => server.post(`${API_ROUTE}/developers`).send(data);

/* Searches for words using the data in MongoDB V2 */
export const getWords = (query = {}, options = {}) =>
export const getWords = (query: Query, options: Options) =>
server
.get(`${API_ROUTE}/words`)
.query(query)
.set('X-API-Key', options.apiKey || FALLBACK_API_KEY);

export const getWord = (id, query = {}, options = {}) =>
export const getWord = (id: Id, query: Query, options: Options) =>
server
.get(`${API_ROUTE}/words/${id}`)
.query(query)
.set('X-API-Key', options.apiKey || FALLBACK_API_KEY);

/* Searches for words using the data in MongoDB V2 */
export const getWordsV2 = (query = {}, options = {}) =>
export const getWordsV2 = (query: Query, options: Options) =>
server
.get(`${API_ROUTE_V2}/words`)
.query(query)
.set('X-API-Key', options.apiKey || FALLBACK_API_KEY);

export const getWordV2 = (id, query = {}, options = {}) =>
export const getWordV2 = (id: Id, query: Query, options: Options) =>
server
.get(`${API_ROUTE_V2}/words/${id}`)
.query(query)
.set('X-API-Key', options.apiKey || FALLBACK_API_KEY);

/* Searches for examples using the data in MongoDB V1 */
export const getExample = (id, query = {}, options = {}) =>
export const getExample = (id: Id, query: Query, options: Options) =>
server
.get(`${API_ROUTE}/examples/${id}`)
.query(query)
.set('X-API-Key', options.apiKey || FALLBACK_API_KEY);

export const getExamples = (query = {}, options = {}) =>
export const getExamples = (query: Query, options: Options) =>
server
.get(`${API_ROUTE}/examples`)
.query(query)
.set('X-API-Key', options.apiKey || FALLBACK_API_KEY);

/* Searches for examples using the data in MongoDB V2 */
export const getExampleV2 = (id, query = {}, options = {}) =>
export const getExampleV2 = (id: Id, query: Query, options: Options) =>
server
.get(`${API_ROUTE_V2}/examples/${id}`)
.query(query)
.set('X-API-Key', options.apiKey || FALLBACK_API_KEY);
export const getExamplesV2 = (query = {}, options = {}) =>
export const getExamplesV2 = (query: Query, options: Options) =>
server
.get(`${API_ROUTE_V2}/examples`)
.query(query)
.set('X-API-Key', options.apiKey || FALLBACK_API_KEY);

/* Searches for Nsibidi characters using the data in MongoDB V1 */
export const getNsibidiCharacter = (id, query = {}, options = {}) =>
export const getNsibidiCharacter = (id: Id, query: Query, options: Options) =>
server
.get(`${API_ROUTE}/nsibidi/${id}`)
.query(query)
.set('X-API-Key', options.apiKey || FALLBACK_API_KEY);

/* Searches for Nsibidi characters using the data in MongoDB V2 */
export const getNsibidiCharacterV2 = (id, query = {}, options = {}) =>
export const getNsibidiCharacterV2 = (id: Id, query: Query, options: Options) =>
server
.get(`${API_ROUTE_V2}/nsibidi/${id}`)
.query(query)
.set('X-API-Key', options.apiKey || FALLBACK_API_KEY);

export const getNsibidiCharactersV2 = (query = {}, options = {}) =>
export const getNsibidiCharactersV2 = (query: Query, options: Options) =>
server
.get(`${API_ROUTE_V2}/nsibidi`)
.query(query)
Expand All @@ -84,12 +105,13 @@ export const getNsibidiCharactersV2 = (query = {}, options = {}) =>
export const populateAPI = () => server.post(`${TEST_ROUTE}/populate`);

/* Uses data in JSON */
export const searchTerm = (term) => server.get(`${TEST_ROUTE}/words`).query({ keyword: term });
export const searchTerm = (term?: string) =>
server.get(`${TEST_ROUTE}/words`).query({ keyword: term || '' });

export const getLocalUrlRoute = (route = LOCAL_ROUTE) => server.get(route);

/* Uses data in __mocks__ folder */
export const searchMockedTerm = (term) => {
export const searchMockedTerm = (term: string) => {
const { wordReg: regexTerm } = createRegExp(term);
return resultsFromDictionarySearch(regexTerm, term, mockedData);
};
File renamed without changes.
7 changes: 3 additions & 4 deletions __tests__/shared/utils.js → __tests__/shared/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import forEach from 'lodash/forEach';
import difference from 'lodash/difference';
import map from 'lodash/map';
import { forEach, difference, map } from 'lodash';
import { expect } from '@jest/globals';

export const expectUniqSetsOfResponses = (res, responseLength = 10) => {
export const expectUniqSetsOfResponses = (res: any[], responseLength = 10) => {
forEach(res, (docsRes, index) => {
expect(docsRes.status).toEqual(200);
expect(docsRes.body.length).toBeLessThanOrEqual(responseLength);
Expand Down
Loading

0 comments on commit e2d68af

Please sign in to comment.