|
| 1 | +import {describe, it} from "mocha"; |
| 2 | +import {expect, use} from "chai"; |
| 3 | +import {keysToSnakeCase} from "../src"; |
| 4 | +import deepEqualInAnyOrder from 'deep-equal-in-any-order'; |
| 5 | + |
| 6 | +use(deepEqualInAnyOrder); |
| 7 | +describe('keysToSnakeCase function', () => { |
| 8 | + it('should be object with snake keys', () => { |
| 9 | + const obj = { |
| 10 | + name: 'test', |
| 11 | + address: { 'street': '123 Street' }, |
| 12 | + |
| 13 | + phones: [ |
| 14 | + { |
| 15 | + countryCode: '123', |
| 16 | + phoneNumber: '2345435' |
| 17 | + }, |
| 18 | + { |
| 19 | + countryCode: '545', |
| 20 | + phoneNumber: '55555', |
| 21 | + isConnectable: 1, |
| 22 | + hasSocial: true, |
| 23 | + socials: ['viber', 'telegram'] |
| 24 | + } |
| 25 | + ], |
| 26 | + products: [], |
| 27 | + hasSubscription: false, |
| 28 | + test: [true], |
| 29 | + }; |
| 30 | + expect(keysToSnakeCase(obj)).to.deep.equal({ |
| 31 | + name: 'test', |
| 32 | + address: { 'street': '123 Street' }, |
| 33 | + additional_emails: ['[email protected]', '[email protected]', null], |
| 34 | + phones: [ |
| 35 | + { |
| 36 | + country_code: '123', |
| 37 | + phone_number: '2345435' |
| 38 | + }, |
| 39 | + { |
| 40 | + country_code: '545', |
| 41 | + phone_number: '55555', |
| 42 | + is_connectable: 1, |
| 43 | + has_social: true, |
| 44 | + socials: ['viber', 'telegram'] |
| 45 | + } |
| 46 | + ], |
| 47 | + products: [], |
| 48 | + has_subscription: false, |
| 49 | + test: [true], |
| 50 | + }) |
| 51 | + }); |
| 52 | + it('should be object array with snake keys', () => { |
| 53 | + const arr = [ |
| 54 | + { |
| 55 | + countryCode: '123', |
| 56 | + phoneNumber: '2345435' |
| 57 | + }, |
| 58 | + { |
| 59 | + code: '545', |
| 60 | + phoneNumber: '55555', |
| 61 | + isConnectable: 1, |
| 62 | + socials: ['viber', 'telegram'] |
| 63 | + } |
| 64 | + ]; |
| 65 | + expect(keysToSnakeCase(arr)).to.deep.equal([ |
| 66 | + { |
| 67 | + country_code: '123', |
| 68 | + phone_number: '2345435' |
| 69 | + }, |
| 70 | + { |
| 71 | + code: '545', |
| 72 | + phone_number: '55555', |
| 73 | + is_connectable: 1, |
| 74 | + socials: ['viber', 'telegram'] |
| 75 | + } |
| 76 | + ]) |
| 77 | + }) |
| 78 | +}) |
0 commit comments