diff --git a/__tests__/shared/fixtures.ts b/__tests__/shared/fixtures.ts deleted file mode 100644 index fe9c8ec9..00000000 --- a/__tests__/shared/fixtures.ts +++ /dev/null @@ -1,96 +0,0 @@ -import { Types } from 'mongoose'; -import { capitalize } from 'lodash'; -import { NextFunction, Request, Response } from 'express'; -import { SuggestionSourceEnum } from '../../src/shared/constants/SuggestionSourceEnum'; -import WordAttributeEnum from '../../src/shared/constants/WordAttributeEnum'; -import { OutgoingExample, OutgoingWord } from '../../src/types'; -import { WordDialect } from '../../src/types/word'; -import { Flags } from '../../src/controllers/utils/types'; -import LanguageEnum from '../../src/shared/constants/LanguageEnum'; - -interface RequestOptions { - noAuthorizationHeader?: boolean; -} - -export const wordFixture = (data?: Partial) => ({ - attributes: Object.values(WordAttributeEnum).reduce( - (finalAttributes, attribute) => ({ ...finalAttributes, [attribute]: false }), - {} - ), - conceptualWord: '', - frequency: 1, - hypernyms: [], - hyponyms: [], - pronunciation: '', - relatedTerms: [], - stems: [], - updatedAt: new Date(), - variations: [], - word: '', - wordPronunciation: '', - definitions: [], - dialects: [], - tags: [], - id: `${new Types.ObjectId()}`, - ...data, -}); - -export const exampleFixture = (data?: Partial) => ({ - id: `${new Types.ObjectId()}`, - associatedDefinitionsSchemas: [], - associatedWords: [], - source: { text: '', language: LanguageEnum.UNSPECIFIED, pronunciations: [] }, - translations: [{ text: '', language: LanguageEnum.UNSPECIFIED, pronunciations: [] }], - meaning: '', - nsibidi: '', - nsibidiCharacters: [], - pronunciations: [], - origin: SuggestionSourceEnum.INTERNAL, - updatedAt: new Date(), - ...data, -}); - -export const dialectFixture = (data?: Partial) => ({ - dialects: [], - id: `${new Types.ObjectId()}`, - pronunciation: '', - variations: [], - word: '', - ...data, -}); - -export const flagsFixture = (data?: Partial) => ({ - examples: false, - dialects: false, - resolve: false, - style: '', - ...data, -}); - -export const requestFixture = ( - { - body = {}, - params = {}, - headers = {}, - }: { - body?: { [key: string]: string }, - params?: { [key: string]: string }, - headers?: { [key: string]: string }, - } = {}, - options?: RequestOptions -): Request => ({ - body, - params, - headers, - query: {}, - // @ts-expect-error get - get: (header: string) => headers[header] || headers[capitalize(header)], -}); -export const statusSendMock = jest.fn(); -export const responseFixture = (): Response => ({ - // @ts-expect-error status - status: jest.fn(() => ({ send: statusSendMock })), - send: jest.fn(), - redirect: jest.fn(), -}); -export const nextFunctionFixture = (): NextFunction => jest.fn(); diff --git a/src/APIs/__tests__/FlagsAPI.test.ts b/src/APIs/__tests__/FlagsAPI.test.ts index ae4fe200..759b7bd8 100644 --- a/src/APIs/__tests__/FlagsAPI.test.ts +++ b/src/APIs/__tests__/FlagsAPI.test.ts @@ -1,17 +1,21 @@ -import { dialectFixture, exampleFixture, wordFixture } from '../../../__tests__/shared/fixtures'; +import { + dialectFixture, + outgoingExampleFixture, + outgoingWordFixture, +} from '../../__tests__/shared/fixtures'; import LanguageEnum from '../../shared/constants/LanguageEnum'; import { SuggestionSourceEnum } from '../../shared/constants/SuggestionSourceEnum'; import { handleWordFlags } from '../FlagsAPI'; describe('FlagsAPI', () => { const words = [ - wordFixture({ + outgoingWordFixture({ word: 'first word', examples: [ - exampleFixture({ + outgoingExampleFixture({ source: { text: 'first example', language: LanguageEnum.IGBO, pronunciations: [] }, }), - exampleFixture({ + outgoingExampleFixture({ source: { text: 'second example', language: LanguageEnum.IGBO, @@ -19,7 +23,7 @@ describe('FlagsAPI', () => { }, origin: SuggestionSourceEnum.INTERNAL, }), - exampleFixture({ + outgoingExampleFixture({ source: { text: 'second example', language: LanguageEnum.IGBO, @@ -29,14 +33,14 @@ describe('FlagsAPI', () => { }), ], }), - wordFixture({ + outgoingWordFixture({ word: 'second word', dialects: [dialectFixture({ word: 'second-word-dialect' })], }), - wordFixture({ + outgoingWordFixture({ word: 'third word', - stems: [wordFixture({ word: 'first stem' })], - relatedTerms: [wordFixture({ word: 'first related term' })], + stems: [outgoingWordFixture({ word: 'first stem' })], + relatedTerms: [outgoingWordFixture({ word: 'first related term' })], }), ]; diff --git a/src/__tests__/shared/fixtures.ts b/src/__tests__/shared/fixtures.ts index 716d35a8..9b409706 100644 --- a/src/__tests__/shared/fixtures.ts +++ b/src/__tests__/shared/fixtures.ts @@ -1,4 +1,9 @@ +import { capitalize } from 'lodash'; +import { WordDialect } from '../../types/word'; +import { NextFunction, Request, Response } from 'express'; +import { Flags } from '../../controllers/utils/types'; import LanguageEnum from '../../shared/constants/LanguageEnum'; +import WordAttributeEnum from '../../shared/constants/WordAttributeEnum'; import { SuggestionSourceEnum } from '../../shared/constants/SuggestionSourceEnum'; import WordClass from '../../shared/constants/WordClass'; import { @@ -7,15 +12,21 @@ import { IncomingWord, DeveloperDocument, DeveloperUsage, + OutgoingExample, + OutgoingWord, } from '../../types'; import { Types } from 'mongoose'; import AccountStatus from '../../shared/constants/AccountStatus'; import ApiType from '../../shared/constants/ApiType'; import Plan from '../../shared/constants/Plan'; +interface RequestOptions { + noAuthorizationHeader?: boolean; +} + export const documentId = new Types.ObjectId('569ed8269353e9f4c51617aa'); -export const wordFixture = (wordData: Partial) => ({ +export const incomingWordFixture = (wordData: Partial) => ({ definitions: [], dialects: [], tags: [], @@ -54,7 +65,7 @@ export const definitionFixture = (definitionData: Partial) => ({ ...definitionData, }); -export const exampleFixture = (exampleData: Partial) => ({ +export const incomingExampleFixture = (exampleData: Partial) => ({ source: { text: '', language: LanguageEnum.UNSPECIFIED, pronunciations: [] }, translations: [{ text: '', language: LanguageEnum.UNSPECIFIED, pronunciations: [] }], meaning: '', @@ -94,3 +105,86 @@ export const developerUsageFixture = (developerFixture: Partial) }, ...developerFixture, }); + +export const outgoingWordFixture = (data?: Partial) => ({ + attributes: Object.values(WordAttributeEnum).reduce( + (finalAttributes, attribute) => ({ ...finalAttributes, [attribute]: false }), + {} + ), + conceptualWord: '', + frequency: 1, + hypernyms: [], + hyponyms: [], + pronunciation: '', + relatedTerms: [], + stems: [], + updatedAt: new Date(), + variations: [], + word: '', + wordPronunciation: '', + definitions: [], + dialects: [], + tags: [], + id: `${new Types.ObjectId()}`, + ...data, +}); + +export const outgoingExampleFixture = (data?: Partial) => ({ + id: `${new Types.ObjectId()}`, + associatedDefinitionsSchemas: [], + associatedWords: [], + source: { text: '', language: LanguageEnum.UNSPECIFIED, pronunciations: [] }, + translations: [{ text: '', language: LanguageEnum.UNSPECIFIED, pronunciations: [] }], + meaning: '', + nsibidi: '', + nsibidiCharacters: [], + pronunciations: [], + origin: SuggestionSourceEnum.INTERNAL, + updatedAt: new Date(), + ...data, +}); + +export const dialectFixture = (data?: Partial) => ({ + dialects: [], + id: `${new Types.ObjectId()}`, + pronunciation: '', + variations: [], + word: '', + ...data, +}); + +export const flagsFixture = (data?: Partial) => ({ + examples: false, + dialects: false, + resolve: false, + style: '', + ...data, +}); + +export const requestFixture = ( + { + body = {}, + params = {}, + headers = {}, + }: { + body?: { [key: string]: string }, + params?: { [key: string]: string }, + headers?: { [key: string]: string }, + } = {}, + options?: RequestOptions +): Request => ({ + body, + params, + headers, + query: {}, + // @ts-expect-error get + get: (header: string) => headers[header] || headers[capitalize(header)], +}); +export const statusSendMock = jest.fn(); +export const responseFixture = (): Response => ({ + // @ts-expect-error status + status: jest.fn(() => ({ send: statusSendMock })), + send: jest.fn(), + redirect: jest.fn(), +}); +export const nextFunctionFixture = (): NextFunction => jest.fn(); diff --git a/src/controllers/__tests__/developers.test.ts b/src/controllers/__tests__/developers.test.ts index 1cabbd9a..68dbeb2d 100644 --- a/src/controllers/__tests__/developers.test.ts +++ b/src/controllers/__tests__/developers.test.ts @@ -2,7 +2,7 @@ import { requestFixture, responseFixture, nextFunctionFixture, -} from '../../../__tests__/shared/fixtures'; +} from '../../__tests__/shared/fixtures'; import { getDeveloper, getDeveloperByEmail, diff --git a/src/controllers/__tests__/examples.test.ts b/src/controllers/__tests__/examples.test.ts index f244cb93..7a74f776 100644 --- a/src/controllers/__tests__/examples.test.ts +++ b/src/controllers/__tests__/examples.test.ts @@ -1,11 +1,11 @@ -import { exampleFixture } from '../../__tests__/shared/fixtures'; +import { incomingExampleFixture } from '../../__tests__/shared/fixtures'; import LanguageEnum from '../../shared/constants/LanguageEnum'; import { SuggestionSourceEnum } from '../../shared/constants/SuggestionSourceEnum'; import { convertToV1Example } from '../examples'; describe('examples', () => { it('converts example pronunciations to pronunciation for v1', () => { - const example = exampleFixture({ + const example = incomingExampleFixture({ source: { text: 'igbo', language: LanguageEnum.IGBO, diff --git a/src/controllers/__tests__/speechToText.test.ts b/src/controllers/__tests__/speechToText.test.ts index c7c1b912..c3a9cbf5 100644 --- a/src/controllers/__tests__/speechToText.test.ts +++ b/src/controllers/__tests__/speechToText.test.ts @@ -3,7 +3,7 @@ import { requestFixture, responseFixture, nextFunctionFixture, -} from '../../../__tests__/shared/fixtures'; +} from '../../__tests__/shared/fixtures'; import { getTranscription } from '../speechToText'; import { fetchBase64Data } from '../utils/fetchBase64Data'; diff --git a/src/controllers/__tests__/stripe.test.ts b/src/controllers/__tests__/stripe.test.ts index 9e32680f..f414266a 100644 --- a/src/controllers/__tests__/stripe.test.ts +++ b/src/controllers/__tests__/stripe.test.ts @@ -1,5 +1,5 @@ jest.mock('stripe'); -import { requestFixture, responseFixture } from '../../../__tests__/shared/fixtures'; +import { requestFixture, responseFixture } from '../../__tests__/shared/fixtures'; import { postCheckoutSession, postPortalSession } from '../stripe'; describe('Credentials', () => { diff --git a/src/controllers/stripe/__tests__/index.test.ts b/src/controllers/stripe/__tests__/index.test.ts index 7b3e3f12..478a5a2a 100644 --- a/src/controllers/stripe/__tests__/index.test.ts +++ b/src/controllers/stripe/__tests__/index.test.ts @@ -3,7 +3,7 @@ import { nextFunctionFixture, requestFixture, responseFixture, -} from '../../../../__tests__/shared/fixtures'; +} from '../../../__tests__/shared/fixtures'; import { handleCustomerSubscriptionCreated, handleCustomerSubscriptionDeleted, diff --git a/src/controllers/utils/__tests__/minimizeVerbsAndSuffixes.test.ts b/src/controllers/utils/__tests__/minimizeVerbsAndSuffixes.test.ts index 6aa10336..09edf356 100644 --- a/src/controllers/utils/__tests__/minimizeVerbsAndSuffixes.test.ts +++ b/src/controllers/utils/__tests__/minimizeVerbsAndSuffixes.test.ts @@ -1,27 +1,27 @@ import Version from '../../../shared/constants/Version'; import WordClass from '../../../shared/constants/WordClass'; -import { definitionFixture, wordFixture } from '../../../__tests__/shared/fixtures'; +import { definitionFixture, incomingWordFixture } from '../../../__tests__/shared/fixtures'; import minimizeVerbsAndSuffixes from '../minimizeVerbsAndSuffixes'; import WordClassEnum from '../../../shared/constants/WordClassEnum'; describe('minimizeVerbsAndSuffixes', () => { it('minimizes the verbs and suffixes to include basic fields', () => { const words = [ - wordFixture({ + incomingWordFixture({ word: 'first word', definitions: [definitionFixture({})], stems: [], relatedTerms: [], id: '123', }), - wordFixture({ + incomingWordFixture({ word: 'second word', definitions: [definitionFixture({ wordClass: WordClassEnum.ADV })], stems: [], relatedTerms: [], id: '456', }), - wordFixture({ + incomingWordFixture({ word: 'third word', definitions: [definitionFixture({ wordClass: WordClassEnum.PREP })], stems: [], diff --git a/src/controllers/utils/__tests__/queries.test.ts b/src/controllers/utils/__tests__/queries.test.ts index ddbadacc..673ccdee 100644 --- a/src/controllers/utils/__tests__/queries.test.ts +++ b/src/controllers/utils/__tests__/queries.test.ts @@ -1,6 +1,6 @@ import createRegExp from '../../../shared/utils/createRegExp'; import { searchExamplesRegexQuery } from '../queries'; -import { flagsFixture } from '../../../../__tests__/shared/fixtures'; +import { flagsFixture } from '../../../__tests__/shared/fixtures'; import { SuggestionSourceEnum } from '../../../shared/constants/SuggestionSourceEnum'; describe('queries', () => { diff --git a/src/middleware/__tests__/authorizeCheckoutSession.test.ts b/src/middleware/__tests__/authorizeCheckoutSession.test.ts index 18d87994..c03ce66c 100644 --- a/src/middleware/__tests__/authorizeCheckoutSession.test.ts +++ b/src/middleware/__tests__/authorizeCheckoutSession.test.ts @@ -3,7 +3,7 @@ import { requestFixture, responseFixture, nextFunctionFixture, -} from '../../../__tests__/shared/fixtures'; +} from '../../__tests__/shared/fixtures'; describe('authorizeCheckoutSession', () => { it('authorizes the current checkout session', async () => { diff --git a/src/middleware/__tests__/authorizePortalSession.test.ts b/src/middleware/__tests__/authorizePortalSession.test.ts index 2c97fe7e..a2797abb 100644 --- a/src/middleware/__tests__/authorizePortalSession.test.ts +++ b/src/middleware/__tests__/authorizePortalSession.test.ts @@ -3,7 +3,7 @@ import { requestFixture, responseFixture, nextFunctionFixture, -} from '../../../__tests__/shared/fixtures'; +} from '../../__tests__/shared/fixtures'; describe('authorizePortalSession', () => { it('authorizes the current portal session', async () => { diff --git a/src/middleware/__tests__/developerAuthorization.test.ts b/src/middleware/__tests__/developerAuthorization.test.ts index c3d43605..9a7dfa5c 100644 --- a/src/middleware/__tests__/developerAuthorization.test.ts +++ b/src/middleware/__tests__/developerAuthorization.test.ts @@ -5,7 +5,7 @@ import { responseFixture, nextFunctionFixture, statusSendMock, -} from '../../../__tests__/shared/fixtures'; +} from '../../__tests__/shared/fixtures'; jest.mock('firebase-admin');