UNPKG

country-currency-phone

Version:

Provides countries, currencies and phone codes base on ISO names, symbols and codes.

285 lines 14.5 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const mocha_1 = require("mocha"); const country_currency_phone_1 = require("../src/country-currency-phone"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const db = JSON.parse(fs.readFileSync(path.join(__dirname, '../src/db.json'), 'utf-8')); function getSampleRecord() { const index = Math.floor(Math.random() * db.length); return db[index]; } function compare(record, expected) { (0, chai_1.expect)(record).to.exist; (0, chai_1.expect)(record === null || record === void 0 ? void 0 : record.country).to.exist; (0, chai_1.expect)(Array.isArray(record === null || record === void 0 ? void 0 : record.country.names)).to.equal(true); (0, chai_1.expect)(record === null || record === void 0 ? void 0 : record.country.names.length).to.equal(expected.country.names.length); record === null || record === void 0 ? void 0 : record.country.names.forEach((name, index) => { (0, chai_1.expect)(name).to.equal(expected.country.names[index]); }); (0, chai_1.expect)(record === null || record === void 0 ? void 0 : record.country.alpha2).to.equal(expected.country.alpha2); (0, chai_1.expect)(record === null || record === void 0 ? void 0 : record.country.alpha3).to.equal(expected.country.alpha3); (0, chai_1.expect)(record === null || record === void 0 ? void 0 : record.currency).to.exist; (0, chai_1.expect)(record === null || record === void 0 ? void 0 : record.currency.name).to.equal(expected.currency.name); (0, chai_1.expect)(record === null || record === void 0 ? void 0 : record.currency.alpha3).to.equal(expected.currency.alpha3); (0, chai_1.expect)(record === null || record === void 0 ? void 0 : record.currency.symbol).to.equal(expected.currency.symbol); (0, chai_1.expect)(Array.isArray(record === null || record === void 0 ? void 0 : record.phoneCodes)).to.equal(true); (0, chai_1.expect)(record === null || record === void 0 ? void 0 : record.phoneCodes.length).to.equal(expected.phoneCodes.length); record === null || record === void 0 ? void 0 : record.phoneCodes.forEach((phoneCode, index) => { (0, chai_1.expect)(phoneCode).to.equal(expected.phoneCodes[index]); }); } (0, mocha_1.describe)('CountryCurrencyPhone', () => { (0, mocha_1.it)('should create an instance of CountryCurrencyPhone', () => { const countryCurrencyPhone = new country_currency_phone_1.CountryCurrencyPhone(); (0, chai_1.expect)(countryCurrencyPhone).to.exist; }); (0, mocha_1.describe)('getByCountryName', () => { let countryCurrencyPhone; let sampleRecord; beforeEach(() => { countryCurrencyPhone = new country_currency_phone_1.CountryCurrencyPhone(); sampleRecord = getSampleRecord(); }); (0, mocha_1.it)('should get record', () => { const record = countryCurrencyPhone.getByCountryName(sampleRecord.country.names[Math.floor(Math.random() * sampleRecord.country.names.length)]); compare(record, sampleRecord); }); (0, mocha_1.it)('should get undefined', () => { const record = countryCurrencyPhone.getByCountryName('Unknown country'); (0, chai_1.expect)(record).to.not.exist; }); }); (0, mocha_1.describe)('getByCountryAlpha2', () => { let countryCurrencyPhone; let sampleRecord; beforeEach(() => { countryCurrencyPhone = new country_currency_phone_1.CountryCurrencyPhone(); sampleRecord = getSampleRecord(); }); (0, mocha_1.it)('should get record', () => { const record = countryCurrencyPhone.getByCountryAlpha2(sampleRecord.country.alpha2); compare(record, sampleRecord); }); (0, mocha_1.it)('should get undefined', () => { const record = countryCurrencyPhone.getByCountryAlpha2('UK'); (0, chai_1.expect)(record).to.not.exist; }); }); (0, mocha_1.describe)('getByCountryAlpha3', () => { let countryCurrencyPhone; let sampleRecord; beforeEach(() => { countryCurrencyPhone = new country_currency_phone_1.CountryCurrencyPhone(); sampleRecord = getSampleRecord(); }); (0, mocha_1.it)('should get record', () => { const record = countryCurrencyPhone.getByCountryAlpha3(sampleRecord.country.alpha3); compare(record, sampleRecord); }); (0, mocha_1.it)('should get undefined', () => { const record = countryCurrencyPhone.getByCountryAlpha3('UnK'); (0, chai_1.expect)(record).to.not.exist; }); }); (0, mocha_1.describe)('getByCurrencyName', () => { let countryCurrencyPhone; let sampleRecord; beforeEach(() => { countryCurrencyPhone = new country_currency_phone_1.CountryCurrencyPhone(); sampleRecord = getSampleRecord(); }); (0, mocha_1.it)('should get records', () => { const records = countryCurrencyPhone.getByCurrencyName(sampleRecord.currency.name); (0, chai_1.expect)(records).to.exist; (0, chai_1.expect)(Array.isArray(records)).to.equal(true); (0, chai_1.expect)(records === null || records === void 0 ? void 0 : records.length).to.greaterThanOrEqual(1); let foundIndex = -1; records === null || records === void 0 ? void 0 : records.forEach((record, index) => { if (record.country.alpha2 === sampleRecord.country.alpha2) { foundIndex = index; } }); (0, chai_1.expect)(foundIndex).to.greaterThanOrEqual(0); }); (0, mocha_1.it)('should get undefined', () => { const records = countryCurrencyPhone.getByCurrencyName('Unknown currency'); (0, chai_1.expect)(records === null || records === void 0 ? void 0 : records.length).to.equal(0); }); }); (0, mocha_1.describe)('getByCurrencyAlpha3', () => { let countryCurrencyPhone; let sampleRecord; beforeEach(() => { countryCurrencyPhone = new country_currency_phone_1.CountryCurrencyPhone(); sampleRecord = getSampleRecord(); }); (0, mocha_1.it)('should get records', () => { const records = countryCurrencyPhone.getByCurrencyAlpha3(sampleRecord.currency.alpha3); (0, chai_1.expect)(records).to.exist; (0, chai_1.expect)(Array.isArray(records)).to.equal(true); (0, chai_1.expect)(records === null || records === void 0 ? void 0 : records.length).to.greaterThanOrEqual(1); let foundIndex = -1; records === null || records === void 0 ? void 0 : records.forEach((record, index) => { if (record.country.alpha2 === sampleRecord.country.alpha2) { foundIndex = index; } }); (0, chai_1.expect)(foundIndex).to.greaterThanOrEqual(0); }); (0, mocha_1.it)('should get undefined', () => { const records = countryCurrencyPhone.getByCurrencyAlpha3('UnK'); (0, chai_1.expect)(records === null || records === void 0 ? void 0 : records.length).to.equal(0); }); }); (0, mocha_1.describe)('getByCurrencySymbol', () => { let countryCurrencyPhone; let sampleRecord; beforeEach(() => { countryCurrencyPhone = new country_currency_phone_1.CountryCurrencyPhone(); sampleRecord = getSampleRecord(); }); (0, mocha_1.it)('should get records', () => { const records = countryCurrencyPhone.getByCurrencySymbol(sampleRecord.currency.symbol); (0, chai_1.expect)(records).to.exist; (0, chai_1.expect)(Array.isArray(records)).to.equal(true); (0, chai_1.expect)(records === null || records === void 0 ? void 0 : records.length).to.greaterThanOrEqual(1); let foundIndex = -1; records === null || records === void 0 ? void 0 : records.forEach((record, index) => { if (record.country.alpha2 === sampleRecord.country.alpha2) { foundIndex = index; } }); (0, chai_1.expect)(foundIndex).to.greaterThanOrEqual(0); }); (0, mocha_1.it)('should get undefined', () => { const records = countryCurrencyPhone.getByCurrencySymbol('Unknown symbol'); (0, chai_1.expect)(records === null || records === void 0 ? void 0 : records.length).to.equal(0); }); }); (0, mocha_1.describe)('getByPhoneCode', () => { let countryCurrencyPhone; let sampleRecord; beforeEach(() => { countryCurrencyPhone = new country_currency_phone_1.CountryCurrencyPhone(); sampleRecord = getSampleRecord(); }); (0, mocha_1.it)('should get records', () => { const records = countryCurrencyPhone.getByPhoneCode(sampleRecord.phoneCodes[Math.floor(Math.random() * sampleRecord.phoneCodes.length)]); (0, chai_1.expect)(records).to.exist; (0, chai_1.expect)(Array.isArray(records)).to.equal(true); (0, chai_1.expect)(records === null || records === void 0 ? void 0 : records.length).to.greaterThanOrEqual(1); let foundIndex = -1; records === null || records === void 0 ? void 0 : records.forEach((record, index) => { if (record.country.alpha2 === sampleRecord.country.alpha2) { foundIndex = index; } }); (0, chai_1.expect)(foundIndex).to.greaterThanOrEqual(0); }); (0, mocha_1.it)('should get undefined', () => { const records = countryCurrencyPhone.getByPhoneCode('+0'); (0, chai_1.expect)(records.length).to.equal(0); }); }); (0, mocha_1.describe)('getAll', () => { let countryCurrencyPhone; beforeEach(() => { countryCurrencyPhone = new country_currency_phone_1.CountryCurrencyPhone(); }); (0, mocha_1.it)('should get all records', () => { const records = countryCurrencyPhone.getAll(); (0, chai_1.expect)(records).to.exist; (0, chai_1.expect)(records.length).to.equal(db.length); }); }); (0, mocha_1.describe)('addToDB', () => { let countryCurrencyPhone; let sampleRecord; beforeEach(() => { countryCurrencyPhone = new country_currency_phone_1.CountryCurrencyPhone(); sampleRecord = getSampleRecord(); }); (0, mocha_1.it)('should add to db', () => { const item = { country: { names: sampleRecord.country.names.concat(['Name 1', 'Name 2']), alpha2: sampleRecord.country.alpha2, alpha3: sampleRecord.country.alpha3, }, currency: { name: sampleRecord.currency.name, alpha3: sampleRecord.currency.alpha3, symbol: sampleRecord.currency.symbol, }, phoneCodes: sampleRecord.phoneCodes, }; let record = countryCurrencyPhone.getByCountryName('Name 1'); (0, chai_1.expect)(record).to.not.exist; countryCurrencyPhone.addToDB(item); record = countryCurrencyPhone.getByCountryName(item.country.names[0]); compare(record, item); }); (0, mocha_1.it)('should throw country alpha2 already exists', () => { const item = { country: { names: ['Name 1', 'Name 2'], alpha2: sampleRecord.country.alpha2, alpha3: 'ZZZ', }, currency: { name: sampleRecord.currency.name, alpha3: sampleRecord.currency.alpha3, symbol: sampleRecord.currency.symbol, }, phoneCodes: ['+123456'], }; const record = countryCurrencyPhone.getByCountryName(item.country.names[0]); (0, chai_1.expect)(record).to.not.exist; (0, chai_1.expect)(countryCurrencyPhone.addToDB.bind(countryCurrencyPhone, item)).to.throw('Country alpha2 already exists!'); }); (0, mocha_1.it)('should throw country alpha3 already exists', () => { const item = { country: { names: ['Name 1', 'Name 2'], alpha2: 'ZZ', alpha3: sampleRecord.country.alpha3, }, currency: { name: sampleRecord.currency.name, alpha3: sampleRecord.currency.alpha3, symbol: sampleRecord.currency.symbol, }, phoneCodes: ['+123456'], }; const record = countryCurrencyPhone.getByCountryName(item.country.names[0]); (0, chai_1.expect)(record).to.not.exist; (0, chai_1.expect)(countryCurrencyPhone.addToDB.bind(countryCurrencyPhone, item)).throw('Country alpha3 already exists!'); }); }); }); //# sourceMappingURL=countryCurrencyPhone.test.js.map