diff --git a/.eslintrc.js b/.eslintrc.js index 01c4d51..5a75f4c 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -2,6 +2,7 @@ module.exports = { 'env': { 'browser': true, 'es6': true, + 'jest/globals': true, }, 'parserOptions': { 'ecmaVersion': 2018, @@ -9,8 +10,10 @@ module.exports = { }, 'plugins': [ 'import', + 'jest', ], 'extends': [ '@nepada', + 'plugin:jest/recommended', ], }; diff --git a/.travis.yml b/.travis.yml index b0f0912..724fff8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,6 +15,8 @@ jobs: install: - travis_retry npm install script: + - npm run lint + - npm run build - npm run test - stage: Code Standard Checker php: 7.4 diff --git a/babel.config.js b/babel.config.js new file mode 100644 index 0000000..23821a0 --- /dev/null +++ b/babel.config.js @@ -0,0 +1,12 @@ +module.exports = { + 'presets': [ + [ + '@babel/preset-env', + { + 'targets': { + 'node': 'current', + }, + }, + ], + ], +}; diff --git a/package.json b/package.json index 1ee236a..3c38016 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,8 @@ "@rollup/plugin-node-resolve": "^6.0.0", "eslint": "^6.8.0", "eslint-plugin-import": "^2.19.1", + "eslint-plugin-jest": "^23.8.2", + "jest": "^24.9.0", "rollup": "^1.27.14", "rollup-plugin-babel": "^4.3.3", "rollup-plugin-node-builtins": "^2.1.2", @@ -46,8 +48,8 @@ "dist" ], "scripts": { - "lint": "eslint src/assets build", - "test": "npm run lint", + "lint": "eslint src/assets build tests/*.js tests/assets", + "test": "jest --config tests/jest.config.js tests/assets/**.test.js", "clean": "rm -rf ./dist/*", "build:cjs": "rollup --config build/rollup.config.cjs.js", "build:umd": "rollup --config build/rollup.config.umd.js", diff --git a/tests/assets/binding.test.js b/tests/assets/binding.test.js new file mode 100644 index 0000000..9e292d5 --- /dev/null +++ b/tests/assets/binding.test.js @@ -0,0 +1,33 @@ +import {testInstances} from './bootstrap'; + +describe.each(testInstances)('%s', (name1, Nette1) => { + describe.each(testInstances)('%s', (name2, Nette2) => { + if (name1 === name2) { + return; + } + + test('getPhoneNumber', () => { + expect(Nette1.getPhoneNumber).toBeDefined(); + expect(Nette1.getPhoneNumber) + .not.toBe(Nette2.getPhoneNumber); + }); + + test('validatePhoneNumber', () => { + expect(Nette1.validators.NepadaPhoneNumberInputValidator_validatePhoneNumber).toBeDefined(); + expect(Nette1.validators.NepadaPhoneNumberInputValidator_validatePhoneNumber) + .not.toBe(Nette2.validators.NepadaPhoneNumberInputValidator_validatePhoneNumber); + }); + + test('validatePhoneNumberStrict', () => { + expect(Nette1.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberStrict).toBeDefined(); + expect(Nette1.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberStrict) + .not.toBe(Nette2.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberStrict); + }); + + test('validatePhoneNumberRegion', () => { + expect(Nette1.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion).toBeDefined(); + expect(Nette1.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion) + .not.toBe(Nette2.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion); + }); + }); +}); diff --git a/tests/assets/bootstrap.cjs.js b/tests/assets/bootstrap.cjs.js new file mode 100644 index 0000000..06ac59f --- /dev/null +++ b/tests/assets/bootstrap.cjs.js @@ -0,0 +1,8 @@ +import metadata from 'libphonenumber-js/metadata.full'; +import init from '../../dist/commonjs/index.cjs'; +import NetteFactory from './netteFactory'; + +const Nette = NetteFactory(); +init(Nette, metadata); + +export default Nette; diff --git a/tests/assets/bootstrap.esm.js b/tests/assets/bootstrap.esm.js new file mode 100644 index 0000000..1dffcc3 --- /dev/null +++ b/tests/assets/bootstrap.esm.js @@ -0,0 +1,8 @@ +import metadata from 'libphonenumber-js/metadata.full'; +import init from '../../src/assets/index.esm'; +import NetteFactory from './netteFactory'; + +const Nette = NetteFactory(); +init(Nette, metadata); + +export default Nette; diff --git a/tests/assets/bootstrap.js b/tests/assets/bootstrap.js new file mode 100644 index 0000000..a540af8 --- /dev/null +++ b/tests/assets/bootstrap.js @@ -0,0 +1,11 @@ +import NetteCjs from './bootstrap.cjs'; +import NetteEsm from './bootstrap.esm'; +import NetteUmd from './bootstrap.umd'; + +const testInstances = [ + ['src/assets/index.esm.js', NetteEsm], + ['dist/commonjs/index.cjs.js', NetteCjs], + ['dist/phone-number-input.js', NetteUmd], +]; + +export {testInstances}; diff --git a/tests/assets/bootstrap.umd.js b/tests/assets/bootstrap.umd.js new file mode 100644 index 0000000..c349e08 --- /dev/null +++ b/tests/assets/bootstrap.umd.js @@ -0,0 +1,4 @@ +import '../../dist/phone-number-input'; +import NetteFactory from './netteFactory'; + +export default NetteFactory(); diff --git a/tests/assets/getPhoneNumber.test.js b/tests/assets/getPhoneNumber.test.js new file mode 100644 index 0000000..5d82177 --- /dev/null +++ b/tests/assets/getPhoneNumber.test.js @@ -0,0 +1,60 @@ +import {testInstances} from './bootstrap'; + +describe.each(testInstances)('%s', (name, Nette) => { + test('no default region', () => { + document.body.innerHTML = '
'; + const form = document.forms[0]; + const input = form.elements[0]; + let phoneNumber; + + phoneNumber = Nette.getPhoneNumber(input); + expect(phoneNumber).toBeNull(); + + input.value = 'abcdef'; + phoneNumber = Nette.getPhoneNumber(input); + expect(phoneNumber).toBeNull(); + + input.value = '800 12 34 56'; + phoneNumber = Nette.getPhoneNumber(input); + expect(phoneNumber).toBeNull(); + + input.value = '+420 800 12 34 56'; + phoneNumber = Nette.getPhoneNumber(input); + expect(phoneNumber.country).toBe('CZ'); + expect(phoneNumber.number).toBe('+420800123456'); + + input.value = '+421 800 12 34 56'; + phoneNumber = Nette.getPhoneNumber(input); + expect(phoneNumber.country).toBe('SK'); + expect(phoneNumber.number).toBe('+421800123456'); + }); + + test('CZ default region', () => { + document.body.innerHTML = ''; + const form = document.forms[0]; + const input = form.elements[0]; + let phoneNumber; + + phoneNumber = Nette.getPhoneNumber(input); + expect(phoneNumber).toBeNull(); + + input.value = 'abcdef'; + phoneNumber = Nette.getPhoneNumber(input); + expect(phoneNumber).toBeNull(); + + input.value = '800 12 34 56'; + phoneNumber = Nette.getPhoneNumber(input); + expect(phoneNumber.country).toBe('CZ'); + expect(phoneNumber.number).toBe('+420800123456'); + + input.value = '+420 800 12 34 56'; + phoneNumber = Nette.getPhoneNumber(input); + expect(phoneNumber.country).toBe('CZ'); + expect(phoneNumber.number).toBe('+420800123456'); + + input.value = '+421 800 12 34 56'; + phoneNumber = Nette.getPhoneNumber(input); + expect(phoneNumber.country).toBe('SK'); + expect(phoneNumber.number).toBe('+421800123456'); + }); +}); diff --git a/tests/assets/netteFactory.js b/tests/assets/netteFactory.js new file mode 100644 index 0000000..5d0609e --- /dev/null +++ b/tests/assets/netteFactory.js @@ -0,0 +1,7 @@ +import Nette from 'nette-forms'; + +export default () => { + const clone = Object.assign({}, Nette); + clone.validators = Object.assign({}, clone.validators); + return clone; +}; diff --git a/tests/assets/validatePhoneNumer.test.js b/tests/assets/validatePhoneNumer.test.js new file mode 100644 index 0000000..3ca54aa --- /dev/null +++ b/tests/assets/validatePhoneNumer.test.js @@ -0,0 +1,49 @@ +import {testInstances} from './bootstrap'; + +describe.each(testInstances)('%s', (name, Nette) => { + test('no default region', () => { + document.body.innerHTML = ''; + const form = document.forms[0]; + const input = form.elements[0]; + + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumber(input)).toBe(false); + + input.value = 'abcdef'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumber(input)).toBe(false); + + input.value = '+42012'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumber(input)).toBe(false); + + input.value = '212 345 678'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumber(input)).toBe(false); + + input.value = '+420 212 345 678'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumber(input)).toBe(true); + + input.value = '+420111111111'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumber(input)).toBe(true); + }); + + test('CZ default region', () => { + document.body.innerHTML = ''; + const form = document.forms[0]; + const input = form.elements[0]; + + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumber(input)).toBe(false); + + input.value = 'abcdef'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumber(input)).toBe(false); + + input.value = '+42012'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumber(input)).toBe(false); + + input.value = '212 345 678'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumber(input)).toBe(true); + + input.value = '+420 212 345 678'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumber(input)).toBe(true); + + input.value = '+420111111111'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumber(input)).toBe(true); + }); +}); diff --git a/tests/assets/validatePhoneNumerRegion.test.js b/tests/assets/validatePhoneNumerRegion.test.js new file mode 100644 index 0000000..88450e1 --- /dev/null +++ b/tests/assets/validatePhoneNumerRegion.test.js @@ -0,0 +1,49 @@ +import {testInstances} from './bootstrap'; + +describe.each(testInstances)('%s', (name, Nette) => { + test('no default region', () => { + document.body.innerHTML = ''; + const form = document.forms[0]; + const input = form.elements[0]; + + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion(input, 'CZ')).toBe(false); + + input.value = 'abcdef'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion(input, 'CZ')).toBe(false); + + input.value = '212 345 678'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion(input, 'CZ')).toBe(false); + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion(input, ['US', 'CZ'])).toBe(false); + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion(input, 'SK')).toBe(false); + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion(input, ['US', 'SK'])).toBe(false); + + input.value = '+421111111111'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion(input, 'CZ')).toBe(false); + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion(input, ['US', 'CZ'])).toBe(false); + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion(input, 'SK')).toBe(true); + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion(input, ['US', 'SK'])).toBe(true); + }); + + test('CZ default region', () => { + document.body.innerHTML = ''; + const form = document.forms[0]; + const input = form.elements[0]; + + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion(input, 'CZ')).toBe(false); + + input.value = 'abcdef'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion(input, 'CZ')).toBe(false); + + input.value = '212 345 678'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion(input, 'CZ')).toBe(true); + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion(input, ['US', 'CZ'])).toBe(true); + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion(input, 'SK')).toBe(false); + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion(input, ['US', 'SK'])).toBe(false); + + input.value = '+421111111111'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion(input, 'CZ')).toBe(false); + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion(input, ['US', 'CZ'])).toBe(false); + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion(input, 'SK')).toBe(true); + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberRegion(input, ['US', 'SK'])).toBe(true); + }); +}); diff --git a/tests/assets/validatePhoneNumerStrict.test.js b/tests/assets/validatePhoneNumerStrict.test.js new file mode 100644 index 0000000..2d1b3bf --- /dev/null +++ b/tests/assets/validatePhoneNumerStrict.test.js @@ -0,0 +1,49 @@ +import {testInstances} from './bootstrap'; + +describe.each(testInstances)('%s', (name, Nette) => { + test('no default region', () => { + document.body.innerHTML = ''; + const form = document.forms[0]; + const input = form.elements[0]; + + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberStrict(input)).toBe(false); + + input.value = 'abcdef'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberStrict(input)).toBe(false); + + input.value = '+42012'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberStrict(input)).toBe(false); + + input.value = '212 345 678'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberStrict(input)).toBe(false); + + input.value = '+420 212 345 678'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberStrict(input)).toBe(true); + + input.value = '+420111111111'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberStrict(input)).toBe(false); + }); + + test('CZ default region', () => { + document.body.innerHTML = ''; + const form = document.forms[0]; + const input = form.elements[0]; + + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberStrict(input)).toBe(false); + + input.value = 'abcdef'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberStrict(input)).toBe(false); + + input.value = '+42012'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberStrict(input)).toBe(false); + + input.value = '212 345 678'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberStrict(input)).toBe(true); + + input.value = '+420 212 345 678'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberStrict(input)).toBe(true); + + input.value = '+420111111111'; + expect(Nette.validators.NepadaPhoneNumberInputValidator_validatePhoneNumberStrict(input)).toBe(false); + }); +}); diff --git a/tests/jest.config.js b/tests/jest.config.js new file mode 100644 index 0000000..14c441a --- /dev/null +++ b/tests/jest.config.js @@ -0,0 +1,4 @@ +/* eslint-disable no-undef */ +module.exports = { + transformIgnorePatterns: [], +};