diff --git a/tests/asKey.test.ts b/tests/asKey.test.ts index a78096f..e3a068f 100644 --- a/tests/asKey.test.ts +++ b/tests/asKey.test.ts @@ -4,7 +4,7 @@ import { getAsKey } from '../src/ISOToLanguage' describe('getAsKey', () => { describe('getAsKey', () => { it('should return an object with languages as keys', () => { - const result = getAsKey('languages') + const result = getAsKey('language') const expectedKeys = Object.keys(result) expect(expectedKeys.length).toBeGreaterThan(10) @@ -14,14 +14,59 @@ describe('getAsKey', () => { const result = getAsKey('name') const expectedKeys = Object.keys(result) - expect(expectedKeys).toEqual(expect.arrayContaining(['Andorra', 'United Arab Emirates', 'Afghanistan'])) + expect(expectedKeys).toEqual( + expect.arrayContaining(['Andorra', 'United Arab Emirates', 'Afghanistan']) + ) + }) + + it('should return an object with locale as keys', () => { + const result = getAsKey('locale') + const expectedKeys = Object.keys(result) + + expect(expectedKeys).toEqual( + expect.arrayContaining([ + 'ca_AD', + 'ar_AE', + 'ps_AF', + 'uz_AF', + 'tk_AF', + 'en_AG', + 'en_AI', + ]) + ) + }) + + it('should return an object with language-code as keys', () => { + const result = getAsKey('language-code') + const expectedKeys = Object.keys(result) + + expect(expectedKeys).toEqual( + expect.arrayContaining([ + 'ca-AD', + 'uz-AF', + 'tk-AF', + 'en-AG', + 'en-AI', + 'sq-AL', + 'hy-AM', + ]) + ) }) it('should return an object with original data as keys', () => { const result = getAsKey('original') const expectedKeys = Object.keys(result) - expect(expectedKeys).toEqual(expect.arrayContaining(["Andorra", "دولة الإمارات العربية المتحدة", "افغانستان", "Antigua and Barbuda", "Anguilla", "Shqipëria"])) + expect(expectedKeys).toEqual( + expect.arrayContaining([ + 'Andorra', + 'دولة الإمارات العربية المتحدة', + 'افغانستان', + 'Antigua and Barbuda', + 'Anguilla', + 'Shqipëria', + ]) + ) }) }) }) diff --git a/tests/format.test.ts b/tests/format.test.ts index c205010..2d11ba3 100644 --- a/tests/format.test.ts +++ b/tests/format.test.ts @@ -1,4 +1,4 @@ -import { format, getCountriesByLanguage } from '../src/ISOToLanguage' +import { format } from '../src/ISOToLanguage' describe('format', () => { it('should format the language and country into a single string with default options', () => { @@ -11,6 +11,26 @@ describe('format', () => { expect(formattedString).toBe('en_US') // assuming en_US is the default country for en }) + it('should return null if the language is not found', () => { + const formattedString = format('hh') + expect(formattedString).toBeNull() + }) + + it('should return null if the language is not provided', () => { + const formattedString = format(undefined as unknown as string) + expect(formattedString).toBeNull() + }) + + it('Should return null if the language is passed at the place of the country', () => { + const formattedString = format('us') + expect(formattedString).toBeNull() + }) + + it('Should fallback to the language if the country is not provided', () => { + const formattedString = format('id') + expect(formattedString).toBe('id_ID') + }) + it('should fallback to the language if the country is not provided and isValidIso returns false', () => { const formattedString = format('EN') expect(formattedString).toBe('en_US') diff --git a/tests/getAll.test.ts b/tests/getAll.test.ts index 31bd603..4c4fdce 100644 --- a/tests/getAll.test.ts +++ b/tests/getAll.test.ts @@ -1,10 +1,8 @@ import { describe, expect } from '@jest/globals' -import {getAll} from '../src/ISOToLanguage' +import { getAll } from '../src/ISOToLanguage' import { isoList } from '../src/iso' describe('getAll', () => { - - describe('getAll', () => { it('should return all ISO codes when type is "iso"', () => { const result = getAll('iso') @@ -22,5 +20,76 @@ describe('getAll', () => { const result = getAll('invalidType' as unknown as 'iso') expect(result).toEqual(isoList) }) + + describe('getAll', () => { + test.each([ + ['iso', Object.keys(isoList)], + ['countries', { AD: {}, AE: {} }], + ['language', ['ar', 'en', 'en', 'en', 'en', 'sq', 'en']], + [ + 'name', + [ + 'Armenia', + 'Angola', + 'Antarctica', + 'Argentina', + 'American Samoa', + 'Austria', + 'Australia', + ], + ], + [ + 'original', + [ + 'دولة الإمارات العربية المتحدة', + 'افغانستان', + 'Antigua and Barbuda', + 'Anguilla', + 'Shqipëria', + ], + ], + [ + 'language-code', + [ + 'ca-AD', + 'ar-AE', + 'ps-AF', + 'uz-AF', + 'tk-AF', + 'en-AG', + 'en-AI', + 'sq-AL', + 'hy-AM', + 'ru-AM', + ], + ], + [ + 'locale', + [ + 'ca_AD', + 'ar_AE', + 'ps_AF', + 'uz_AF', + 'tk_AF', + 'en_AG', + 'en_AI', + 'sq_AL', + 'hy_AM', + 'ru_AM', + ], + ], + ])('should return correct result for type "%s"', (type, expected) => { + // Run the test + const result = getAll(type) + + if (type !== 'countries') { + // Test only a part of the result array + expect(result).toEqual(expect.arrayContaining(expected)) + } else { + // For other types, test objects + expect(result).toMatchObject(expected) + } + }) + }) }) }) diff --git a/tests/getAllLanguageCodesByISO.test.ts b/tests/getAllLanguageCodesByISO.test.ts index 9ce9e90..9afe656 100644 --- a/tests/getAllLanguageCodesByISO.test.ts +++ b/tests/getAllLanguageCodesByISO.test.ts @@ -1,17 +1,13 @@ import { describe, expect } from '@jest/globals' -import {getAllLanguagesByISO} from '../src/ISOToLanguage' +import { getAllLanguagesByISO } from '../src/ISOToLanguage' import { isoList } from '../src/iso' describe('getAllLanguagesByISO', () => { - - it('should return all languages associated with the given ISO codes', () => { const isoCodes = ['AD', 'AE', 'AF'] const result = getAllLanguagesByISO(isoCodes) const expectedLanguages = Array.from( - new Set( - isoCodes.flatMap((isoCode) => isoList[isoCode].languages) - ) + new Set(isoCodes.flatMap((isoCode) => isoList[isoCode].languages)) ) expect(result).toEqual(expectedLanguages) diff --git a/tests/getAllLanguagesByISO.test.ts b/tests/getAllLanguagesByISO.test.ts index 7bd27ca..da79198 100644 --- a/tests/getAllLanguagesByISO.test.ts +++ b/tests/getAllLanguagesByISO.test.ts @@ -3,17 +3,13 @@ import { getAllLanguageCodesByISO } from '../src/ISOToLanguage' import { isoList } from '../src/iso' describe('getAllLanguageCodesByISO', () => { - - it('should return all language codes associated with the given ISO codes', () => { const isoCodes = ['AD', 'AE', 'AF'] const result = getAllLanguageCodesByISO(isoCodes) const expectedLanguageCodes = Array.from( new Set( isoCodes.flatMap((isoCode) => { - return isoList[isoCode].languages.map( - (language) => `${language}-${isoCode}` - ) + return isoList[isoCode].languages.map((language) => `${language}-${isoCode}`) }) ) ) @@ -27,9 +23,7 @@ describe('getAllLanguageCodesByISO', () => { const expectedLanguageCodes = Array.from( new Set( isoCodes.flatMap((isoCode) => { - return isoList[isoCode].languages.map( - (language) => `${language}_${isoCode}` - ) + return isoList[isoCode].languages.map((language) => `${language}_${isoCode}`) }) ) ) diff --git a/tests/getCountriesByLanguage.test.ts b/tests/getCountriesByLanguage.test.ts index 529c695..df58df1 100644 --- a/tests/getCountriesByLanguage.test.ts +++ b/tests/getCountriesByLanguage.test.ts @@ -1,4 +1,4 @@ -import {getCountriesByLanguage} from '../src/ISOToLanguage' +import { getCountriesByLanguage } from '../src/ISOToLanguage' describe('getCountriesByLanguage', () => { it('should return an array of countries that speak the given languages', () => { @@ -7,13 +7,15 @@ describe('getCountriesByLanguage', () => { // Add your expectations based on the isoList data // For demonstration purposes we assume that isoList contains specific data - expect(result).toEqual(expect.objectContaining({ - AG: { - languages: ['en'], - name: 'Antigua and Barbuda', - original: 'Antigua and Barbuda', - }, - } )) + expect(result).toEqual( + expect.objectContaining({ + AG: { + languages: ['en'], + name: 'Antigua and Barbuda', + original: 'Antigua and Barbuda', + }, + }) + ) }) it('should handle an empty array of languages', () => { diff --git a/tests/getCountry.test.ts b/tests/getCountry.test.ts index d5e1b3a..aa74982 100644 --- a/tests/getCountry.test.ts +++ b/tests/getCountry.test.ts @@ -11,9 +11,7 @@ describe('ISOToLanguage', () => { name: 'Andorra', original: 'Andorra', } - const resultByOriginal = getCountry( - 'دولة الإمارات العربية المتحدة' - ) + const resultByOriginal = getCountry('دولة الإمارات العربية المتحدة') const expectedDataAE = { code: 'AE', languages: ['ar'], @@ -23,9 +21,7 @@ describe('ISOToLanguage', () => { expect(resultByName).toEqual(expectedDataAD) expect(resultByOriginal).not.toBeNull() - expect(resultByOriginal).toEqual( - expect.objectContaining(expectedDataAE) - ) + expect(resultByOriginal).toEqual(expect.objectContaining(expectedDataAE)) }) it('should return null for an invalid country name', () => { diff --git a/tests/getCountryData.test.ts b/tests/getCountryData.test.ts index 4ea004b..e9a4693 100644 --- a/tests/getCountryData.test.ts +++ b/tests/getCountryData.test.ts @@ -2,7 +2,6 @@ import { describe, expect } from '@jest/globals' import { getCountryData } from '../src/ISOToLanguage' describe('ISOToLanguage', () => { - it('should return the country data for a valid language code', () => { const result = getCountryData('tk_AF') const expectedData = { @@ -19,7 +18,6 @@ describe('ISOToLanguage', () => { expect(result).toBeFalsy() }) - it('should return the country data for a valid language code', () => { const result = getCountryData('AF_tk') const expectedData = { @@ -31,6 +29,17 @@ describe('ISOToLanguage', () => { expect(result).not.toEqual(expectedData) }) + it('should return the country data for a valid language code', () => { + const result = getCountryData('IT') + const expectedData = { + languages: ['it'], + name: 'Italy', + original: 'Italia', + } + + expect(result).toEqual(expectedData) + }) + it('should return null for an invalid language code', () => { const result = getCountryData(false as unknown as string) diff --git a/tests/getCountryIso.test.ts b/tests/getCountryIso.test.ts index b78c737..075fa7a 100644 --- a/tests/getCountryIso.test.ts +++ b/tests/getCountryIso.test.ts @@ -2,7 +2,6 @@ import { describe, expect } from '@jest/globals' import { getCountriesByISO } from '../src/ISOToLanguage' describe('getCountriesByISO', () => { - it('should return an object of country data for valid ISO codes', () => { const result = getCountriesByISO(['AD', 'AE', 'AF']) const expectedData = { @@ -27,10 +26,7 @@ describe('getCountriesByISO', () => { }) it('should return an empty object for an array of invalid ISO codes', () => { - const result = getCountriesByISO([ - 'Invalid1', - 'Invalid2', - ]) + const result = getCountriesByISO(['Invalid1', 'Invalid2']) expect(result).toEqual({}) }) diff --git a/tests/getKeyValue .test.ts b/tests/getKeyValue .test.ts new file mode 100644 index 0000000..01aa1d4 --- /dev/null +++ b/tests/getKeyValue .test.ts @@ -0,0 +1,13 @@ +import { describe, expect, test } from '@jest/globals'; +import { getKeyValue } from '../src/ISOToLanguage'; +import { IsoDataType } from '../src/type' + +describe('getKeyValue', () => { + test.each([ + ['iso', 'name', [{ value: 'AD', label: 'Andorra' }, /* ... */]], + ['language', 'original', [{ value: 'nl', label: 'Nederland' }, /* ... */]], + ])('should return the expected array for key "%s" and value "%s"', (key, value, expected) => { + const result = getKeyValue(key as IsoDataType, value as IsoDataType); + expect(result).toEqual(expect.arrayContaining(expected)) + }); +}); diff --git a/tests/isValid.test.ts b/tests/isValid.test.ts index 2d63520..ae2a808 100644 --- a/tests/isValid.test.ts +++ b/tests/isValid.test.ts @@ -2,8 +2,6 @@ import { describe, expect } from '@jest/globals' import { isValidIso } from '../src/ISOToLanguage' describe('ISOToLanguage', () => { - - describe('isValidISO', () => { it('should return true for a valid ISO code', () => { const result = isValidIso('AD') diff --git a/tests/get.test.ts b/tests/iso.test.ts similarity index 74% rename from tests/get.test.ts rename to tests/iso.test.ts index ffc8d00..89410a2 100644 --- a/tests/get.test.ts +++ b/tests/iso.test.ts @@ -1,24 +1,20 @@ import { describe, expect } from '@jest/globals' -import { iso } from '../src/ISOToLanguage' +import { ISO } from '../src/ISOToLanguage' import { isoList } from '../src/iso' describe('get', () => { - - describe('getByIso', () => { it('should return languages for the provided ISO code and type "languages"', () => { const isoCode = 'AD' - const result = iso(isoCode, 'languages') - const expectedLanguages = isoList[isoCode].languages.map( - (language) => `${language}` - ) + const result = ISO(isoCode, 'language') + const expectedLanguages = isoList[isoCode].languages.map((language) => `${language}`) expect(result).toEqual(expectedLanguages) }) it('should return languages for the provided ISO code and type "locale"', () => { const isoCode = 'AD' - const result = iso(isoCode, 'locale') + const result = ISO(isoCode, 'locale') const expectedLanguages = isoList[isoCode].languages.map( (language) => `${language}_${isoCode}` ) @@ -28,7 +24,7 @@ describe('get', () => { it('should return languages for the provided ISO code and type "language-code"', () => { const isoCode = 'AD' - const result = iso(isoCode, 'language-code') + const result = ISO(isoCode, 'language-code') const expectedLanguages = isoList[isoCode].languages.map( (language) => `${language}-${isoCode}` ) @@ -36,9 +32,35 @@ describe('get', () => { expect(result).toEqual(expectedLanguages) }) + it('should return false for an invalid type', () => { + const isoCode = 'AD' + const result = ISO(isoCode, 'invalidType' as unknown as 'locale') + + expect(result).toMatchObject({ + languages: ['ca'], + name: 'Andorra', + original: 'Andorra', + }) + }) + + it('should return ISO data for a valid ISO code', () => { + const validISO = 'AD' + const result = ISO(validISO) + const expectedData = isoList[validISO] + + expect(result).toEqual(expectedData) + }) + + it('should return ISO data for a valid ISO code', () => { + const validISO = 'HH' + const result = ISO(validISO) + + expect(result).toBeFalsy() + }) + it('should return languages for the provided ISO code and type "languages"', () => { const isoCode = 'AD' - const result = iso(isoCode, 'locale') + const result = ISO(isoCode, 'locale') const expectedLanguages = isoList[isoCode].languages.map( (language) => `${language}_${isoCode}` ) @@ -48,7 +70,7 @@ describe('get', () => { it('should return name for the provided ISO code and type "names"', () => { const isoCode = 'AD' - const result = iso(isoCode, 'names') + const result = ISO(isoCode, 'name') const expectedName = isoList[isoCode].name expect(result).toEqual(expectedName) @@ -56,7 +78,7 @@ describe('get', () => { it('should return original data for the provided ISO code and type "original"', () => { const isoCode = 'AD' - const result = iso(isoCode, 'original') + const result = ISO(isoCode, 'original') const expectedOriginal = isoList[isoCode].original expect(result).toEqual(expectedOriginal) @@ -64,7 +86,7 @@ describe('get', () => { it('should return ISO data for the provided ISO code and no type', () => { const isoCode = 'AD' - const result = iso(isoCode) + const result = ISO(isoCode) const expectedData = isoList[isoCode] expect(result).toEqual(expectedData) @@ -72,34 +94,15 @@ describe('get', () => { it('should return ISO data for the provided ISO code and no type', () => { const isoCode = null - const result = iso(isoCode as unknown as string) + const result = ISO(isoCode as unknown as string) const expectedData = false expect(result).toEqual(expectedData) }) - it('should return false for an invalid type', () => { - const isoCode = 'AD' - const result = iso(isoCode, 'invalidType' as unknown as 'locale') - - expect(result).toMatchObject({ - languages: ['ca'], - name: 'Andorra', - original: 'Andorra', - }) - }) - - it('should return ISO data for a valid ISO code', () => { - const validISO = 'AD' - const result = iso(validISO) - const expectedData = isoList[validISO] - - expect(result).toEqual(expectedData) - }) - it('should return ISO data for a valid ISO code', () => { const validISO = 'IT' - const result = iso(validISO) + const result = ISO(validISO) const expectedData = { languages: ['it'], name: 'Italy', @@ -109,35 +112,42 @@ describe('get', () => { expect(result).toEqual(expectedData) }) + it('should return languages for the provided ISO code and type "languages"', () => { + const isoCode = undefined + const result = ISO(isoCode as unknown as string, 'language') + + expect(result).toBeFalsy() + }) + it('should return false for an invalid ISO code', () => { const invalidISO = 'InvalidCode' - const result = iso(invalidISO) + const result = ISO(invalidISO) expect(result).toBe(false) }) it('should return false for an empty ISO code', () => { const emptyISO = '' - const result = iso(emptyISO) + const result = ISO(emptyISO) expect(result).toBe(false) }) it('should return false for undefined ISO code', () => { - const result = iso(undefined as unknown as string) + const result = ISO(undefined as unknown as string) expect(result).toBe(false) }) it('should return false for null ISO code', () => { - const result = iso(null as unknown as string) + const result = ISO(null as unknown as string) expect(result).toBe(false) }) it('should return false for a non-string ISO code', () => { const nonStringISO = 123 - const result = iso(nonStringISO as unknown as string) + const result = ISO(nonStringISO as unknown as string) expect(result).toBe(false) }) diff --git a/tests/tsconfig.test.json b/tests/tsconfig.test.json index d638db9..df7fa50 100644 --- a/tests/tsconfig.test.json +++ b/tests/tsconfig.test.json @@ -1,24 +1,14 @@ { - "transformIgnorePatterns": [ - "/node_modules/" - ], - "compilerOptions": { - "moduleResolution": "nodeNext", - "target": "ESNext", - "strictNullChecks": true, - "esModuleInterop": true - }, - "coverageThreshold": { - "global": { - "branches": 10, - "functions": 10, - "lines": 100, - "statements": 10 - } - }, - "transform": { - "\\.[jt]sx?$": "babel-jest" - }, - "testRegex": "(/tests/.*|(\\.|/)(test|spec))\\.tsx?$", - "collectCoverage": true + "transformIgnorePatterns": ["/node_modules/"], + "compilerOptions": { + "moduleResolution": "nodeNext", + "target": "ESNext", + "strictNullChecks": true, + "esModuleInterop": true + }, + "transform": { + "\\.[jt]sx?$": "babel-jest" + }, + "testRegex": "(/tests/.*|(\\.|/)(test|spec))\\.tsx?$", + "collectCoverage": true }