|
| 1 | +// biome-ignore-all lint/performance/noDynamicNamespaceImportAccess: test file |
| 2 | + |
| 3 | +import { describe, expect, test } from 'bun:test' |
| 4 | +import fs from 'node:fs' |
| 5 | +import * as source from 'src/index.ts' |
| 6 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 7 | +// @ts-expect-error |
| 8 | +import * as mjs from '../../dist/mjs/index.js' |
| 9 | + |
| 10 | +const exportDataList: Partial<keyof typeof source>[] = ['continents', 'countries', 'languages'] |
| 11 | +const exportFnList: Partial<keyof typeof source>[] = ['getEmojiFlag'] |
| 12 | + |
| 13 | +function evalIIFE(name = 'Countries') { |
| 14 | + const script = fs.readFileSync('../../dist/index.iife.js', { encoding: 'utf-8' }) |
| 15 | + // biome-ignore lint/security/noGlobalEval: - |
| 16 | + eval(`this.${name} = (function () { ${script}\nreturn ${name}})()`) |
| 17 | +} |
| 18 | + |
| 19 | +describe('dist', () => { |
| 20 | + test('has proper CJS ES6 export', () => { |
| 21 | + expect(typeof mjs).toBe('object') |
| 22 | + |
| 23 | + for (const prop of exportFnList) { |
| 24 | + expect(Object.hasOwn(mjs, prop)).toBe(true) |
| 25 | + } |
| 26 | + |
| 27 | + for (const prop of exportDataList) { |
| 28 | + expect(Object.hasOwn(mjs, prop)).toBe(true) |
| 29 | + |
| 30 | + const cjsKeys = Object.keys(mjs[prop]) |
| 31 | + const srcKeys = Object.keys(source[prop]) |
| 32 | + expect(cjsKeys).toEqual(srcKeys) |
| 33 | + } |
| 34 | + }) |
| 35 | + |
| 36 | + test('all English country names should contain only A-Z characters', () => { |
| 37 | + const nameReg = /^[a-z\s.()-]+$/i |
| 38 | + const nonAZ: string[] = [] |
| 39 | + for (const c of Object.values(source.countries)) { |
| 40 | + if (!nameReg.test(c.name)) { |
| 41 | + nonAZ.push(c.name) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // It helps see incorrect names right away in logs |
| 46 | + expect(nonAZ).toEqual([]) |
| 47 | + }) |
| 48 | + |
| 49 | + test('loads ES6 <script> properly', () => { |
| 50 | + const context: { Countries?: typeof source } = {} |
| 51 | + evalIIFE.call(context) |
| 52 | + |
| 53 | + const contextCountries = context.Countries |
| 54 | + |
| 55 | + expect(contextCountries).toBeTruthy() |
| 56 | + expect(contextCountries).toBeObject() |
| 57 | + |
| 58 | + for (const prop of exportFnList) { |
| 59 | + expect(contextCountries).toHaveProperty(prop) |
| 60 | + } |
| 61 | + |
| 62 | + for (const prop of exportDataList) { |
| 63 | + expect(contextCountries).toHaveProperty(prop) |
| 64 | + |
| 65 | + // biome-ignore lint/style/noNonNullAssertion: - |
| 66 | + const windowProps = Object.keys(contextCountries![prop]) as string[] |
| 67 | + const dataProps = Object.keys(source[prop]) as string[] |
| 68 | + expect(windowProps).toEqual(dataProps) |
| 69 | + } |
| 70 | + }) |
| 71 | +}) |
0 commit comments