|
| 1 | +const expect = require('chai').expect; |
| 2 | + |
| 3 | +const jnestedReplace = require('../index'); |
| 4 | + |
| 5 | +const INPUT_JSON = { |
| 6 | + 'name': 'json-nested-replace', |
| 7 | + 'author': 'Arshad Kazmi', |
| 8 | + 'repository': { |
| 9 | + 'url': 'https://github.com/arshadkazmi42/json-nested-replace', |
| 10 | + 'language': 'js' |
| 11 | + } |
| 12 | +}; |
| 13 | + |
| 14 | +const REPLACED_JSON = { |
| 15 | + 'name': 'jnested-replace', |
| 16 | + 'author': 'Arshad Kazmi', |
| 17 | + 'repository': { |
| 18 | + 'url': 'https://github.com/arshadkazmi42/jnested-replace', |
| 19 | + 'language': 'js' |
| 20 | + } |
| 21 | +}; |
| 22 | + |
| 23 | +const ARRAY_JSON = { |
| 24 | + 'name': 'json-nested-replace', |
| 25 | + 'author': 'Arshad Kazmi', |
| 26 | + 'repository': { |
| 27 | + 'url': 'https://github.com/arshadkazmi42/jnested-replace', |
| 28 | + 'language': 'js' |
| 29 | + }, |
| 30 | + 'keywords': [ |
| 31 | + 'json-nested', |
| 32 | + 'json-nested-replace', |
| 33 | + 'json' |
| 34 | + ], |
| 35 | + 'related': [ |
| 36 | + { |
| 37 | + 'name': 'jmon' |
| 38 | + }, |
| 39 | + { |
| 40 | + 'name': 'json-nested-find-and-replace' |
| 41 | + } |
| 42 | + ] |
| 43 | +}; |
| 44 | + |
| 45 | +const REPLACE_ARRYA_JSON = { |
| 46 | + 'name': 'jnested-replace', |
| 47 | + 'author': 'Arshad Kazmi', |
| 48 | + 'repository': { |
| 49 | + 'url': 'https://github.com/arshadkazmi42/jnested-replace', |
| 50 | + 'language': 'js' |
| 51 | + }, |
| 52 | + 'keywords': [ |
| 53 | + 'jnested', |
| 54 | + 'jnested-replace', |
| 55 | + 'json' |
| 56 | + ], |
| 57 | + 'related': [ |
| 58 | + { |
| 59 | + 'name': 'jmon' |
| 60 | + }, |
| 61 | + { |
| 62 | + 'name': 'jnested-find-and-replace' |
| 63 | + } |
| 64 | + ] |
| 65 | +}; |
| 66 | + |
| 67 | + |
| 68 | +describe('replace in nested json', () => { |
| 69 | + it('should replace all the occurences in object', () => { |
| 70 | + const replacedValue = jnestedReplace(INPUT_JSON, 'json-nested-replace', 'jnested-replace') |
| 71 | + expect(replacedValue).to.deep.equal(REPLACED_JSON) |
| 72 | + }); |
| 73 | + it('should replace all the occurences in string', () => { |
| 74 | + const replacedValue = jnestedReplace(INPUT_JSON.name, 'json-nested-replace', 'jnested-replace') |
| 75 | + expect(replacedValue).to.deep.equal(REPLACED_JSON.name) |
| 76 | + }); |
| 77 | + it('should replace all the occurences in object and array', () => { |
| 78 | + const replacedValue = jnestedReplace(ARRAY_JSON, 'json-nested', 'jnested') |
| 79 | + expect(replacedValue).to.deep.equal(REPLACE_ARRYA_JSON) |
| 80 | + }); |
| 81 | + it('should throw error of empty input values', () => { |
| 82 | + try { |
| 83 | + jnestedReplace() |
| 84 | + } catch (err) { |
| 85 | + expect(err).to.equal('JSON, searchValue, newValue cannot be null'); |
| 86 | + } |
| 87 | + }); |
| 88 | +}); |
0 commit comments