Skip to content

Commit

Permalink
100% coverage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
erikyo committed Dec 17, 2023
1 parent 71c32b6 commit c09c6df
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 105 deletions.
51 changes: 48 additions & 3 deletions tests/asKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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',
])
)
})
})
})
22 changes: 21 additions & 1 deletion tests/format.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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')
Expand Down
75 changes: 72 additions & 3 deletions tests/getAll.test.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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)
}
})
})
})
})
8 changes: 2 additions & 6 deletions tests/getAllLanguageCodesByISO.test.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
10 changes: 2 additions & 8 deletions tests/getAllLanguagesByISO.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
})
)
)
Expand All @@ -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}`)
})
)
)
Expand Down
18 changes: 10 additions & 8 deletions tests/getCountriesByLanguage.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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', () => {
Expand Down
8 changes: 2 additions & 6 deletions tests/getCountry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ describe('ISOToLanguage', () => {
name: 'Andorra',
original: 'Andorra',
}
const resultByOriginal = getCountry(
'دولة الإمارات العربية المتحدة'
)
const resultByOriginal = getCountry('دولة الإمارات العربية المتحدة')
const expectedDataAE = {
code: 'AE',
languages: ['ar'],
Expand All @@ -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', () => {
Expand Down
13 changes: 11 additions & 2 deletions tests/getCountryData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand All @@ -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)

Expand Down
6 changes: 1 addition & 5 deletions tests/getCountryIso.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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({})
})
Expand Down
13 changes: 13 additions & 0 deletions tests/getKeyValue .test.ts
Original file line number Diff line number Diff line change
@@ -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))
});
});
2 changes: 0 additions & 2 deletions tests/isValid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Loading

0 comments on commit c09c6df

Please sign in to comment.