|
| 1 | +chai = require 'chai' |
| 2 | +sinonChai = require 'sinon-chai' |
| 3 | +sinon = require 'sinon' |
| 4 | +deepMapKeys = require '../lib' |
| 5 | + |
| 6 | +before -> |
| 7 | + chai.use sinonChai |
| 8 | + chai.should() |
| 9 | + |
| 10 | +err = (fn, ctor) -> |
| 11 | + fn.should.throw ctor |
| 12 | + |
| 13 | +describe 'deepMapKeys(object, transformFn, [options])', -> |
| 14 | + snakeToCamel = null |
| 15 | + camelMap = null |
| 16 | + |
| 17 | + beforeEach -> |
| 18 | + snakeToCamel = sinon.spy (str) -> |
| 19 | + str.replace /_(\w)/g, (match, char) -> |
| 20 | + char.toUpperCase() |
| 21 | + |
| 22 | + camelMap = (object, options) -> |
| 23 | + deepMapKeys object, snakeToCamel, options |
| 24 | + |
| 25 | + it 'is the main module', -> |
| 26 | + deepMapKeys.should.equal require('..') |
| 27 | + |
| 28 | + it 'is a function', -> |
| 29 | + deepMapKeys.should.be.a 'function' |
| 30 | + |
| 31 | + context 'simple object is passed', -> |
| 32 | + |
| 33 | + it 'transforms keys', -> |
| 34 | + camelMap |
| 35 | + user_id: 42 |
| 36 | + user_name: 'Mufasa' |
| 37 | + .should.deep.equal |
| 38 | + userId: 42 |
| 39 | + userName: 'Mufasa' |
| 40 | + |
| 41 | + context 'nested object is passed', -> |
| 42 | + |
| 43 | + it 'transforms keys', -> |
| 44 | + camelMap |
| 45 | + user_info: |
| 46 | + user_id: 42 |
| 47 | + user_name: 'Mufasa' |
| 48 | + .should.deep.equal |
| 49 | + userInfo: |
| 50 | + userId: 42 |
| 51 | + userName: 'Mufasa' |
| 52 | + |
| 53 | + context 'complex object is passed', -> |
| 54 | + |
| 55 | + it 'transforms keys', -> |
| 56 | + camelMap |
| 57 | + user_id: 42 |
| 58 | + user_name: 'Mufasa' |
| 59 | + viewed_by: [ |
| 60 | + user_id: 100, user_name: 'Rafiki' |
| 61 | + , |
| 62 | + user_id: 101, user_name: 'Zazu' |
| 63 | + ] |
| 64 | + .should.deep.equal |
| 65 | + userId: 42 |
| 66 | + userName: 'Mufasa' |
| 67 | + viewedBy: [ |
| 68 | + userId: 100, userName: 'Rafiki' |
| 69 | + , |
| 70 | + userId: 101, userName: 'Zazu' |
| 71 | + ] |
| 72 | + |
| 73 | + context 'complex array is passed', -> |
| 74 | + |
| 75 | + it 'transforms keys', -> |
| 76 | + camelMap [ |
| 77 | + user_id: 100, user_name: 'Rafiki' |
| 78 | + , |
| 79 | + user_id: 101, user_name: 'Zazu' |
| 80 | + ] |
| 81 | + .should.deep.equal [ |
| 82 | + userId: 100, userName: 'Rafiki' |
| 83 | + , |
| 84 | + userId: 101, userName: 'Zazu' |
| 85 | + ] |
| 86 | + |
| 87 | + context '@options.thisArg is set', -> |
| 88 | + |
| 89 | + it 'sets context in @transformFn', -> |
| 90 | + camelMap({user_id: 41}, {thisArg: 42}) |
| 91 | + snakeToCamel.should.have.been.calledOn 42 |
| 92 | + |
| 93 | + context 'non-object is passed as @options', -> |
| 94 | + |
| 95 | + it 'throws TypeError', -> |
| 96 | + err((-> camelMap({user_id: 42}, 42)), TypeError) |
| 97 | + |
| 98 | + context 'non-function is passed as @transformFn', -> |
| 99 | + |
| 100 | + it 'throws TypeError', -> |
| 101 | + err((-> deepMapKeys({user_id: 42}, 42)), TypeError) |
| 102 | + |
| 103 | + context 'undefined @transformFn', -> |
| 104 | + |
| 105 | + it 'throws Error', -> |
| 106 | + err((-> deepMapKeys({user_id: 42})), Error) |
| 107 | + |
| 108 | + describe 'return value', -> |
| 109 | + |
| 110 | + it 'is a new object', -> |
| 111 | + obj = user_id: 42 |
| 112 | + camelMap(obj).should.not.equal obj |
| 113 | + obj.should.deep.equal user_id: 42 |
0 commit comments