|
| 1 | +/* eslint-disable no-unused-expressions */ |
| 2 | +const chai = require('chai'); |
| 3 | + |
| 4 | +const { protect } = require('../../lib/hooks'); |
| 5 | +const { expect } = chai; |
| 6 | + |
| 7 | +function testOmit (title, property) { |
| 8 | + describe(title, () => { |
| 9 | + const fn = protect('password'); |
| 10 | + |
| 11 | + it('omits from object', () => { |
| 12 | + const data = { |
| 13 | + email: 'test@user.com', |
| 14 | + password: 'supersecret' |
| 15 | + }; |
| 16 | + const context = { |
| 17 | + [property]: data |
| 18 | + }; |
| 19 | + const result = fn(context); |
| 20 | + |
| 21 | + expect(result).to.deep.equal({ |
| 22 | + [property]: data, |
| 23 | + dispatch: { email: 'test@user.com' } |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + it('omits from array', () => { |
| 28 | + const data = [{ |
| 29 | + email: 'test1@user.com', |
| 30 | + password: 'supersecret' |
| 31 | + }, { |
| 32 | + email: 'test2@user.com', |
| 33 | + password: 'othersecret' |
| 34 | + }]; |
| 35 | + const context = { |
| 36 | + [property]: data |
| 37 | + }; |
| 38 | + const result = fn(context); |
| 39 | + |
| 40 | + expect(result).to.deep.equal({ |
| 41 | + [property]: data, |
| 42 | + dispatch: [ |
| 43 | + { email: 'test1@user.com' }, |
| 44 | + { email: 'test2@user.com' } |
| 45 | + ] |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('omits from pagination object', () => { |
| 50 | + const data = { |
| 51 | + total: 2, |
| 52 | + data: [{ |
| 53 | + email: 'test1@user.com', |
| 54 | + password: 'supersecret' |
| 55 | + }, { |
| 56 | + email: 'test2@user.com', |
| 57 | + password: 'othersecret' |
| 58 | + }] |
| 59 | + }; |
| 60 | + const context = { |
| 61 | + [property]: data |
| 62 | + }; |
| 63 | + const result = fn(context); |
| 64 | + |
| 65 | + expect(result).to.deep.equal({ |
| 66 | + [property]: data, |
| 67 | + dispatch: { |
| 68 | + total: 2, |
| 69 | + data: [ |
| 70 | + { email: 'test1@user.com' }, |
| 71 | + { email: 'test2@user.com' } |
| 72 | + ] |
| 73 | + } |
| 74 | + }); |
| 75 | + }); |
| 76 | + }); |
| 77 | +} |
| 78 | + |
| 79 | +describe('hooks:protect', () => { |
| 80 | + it('does nothing when called with no result', () => { |
| 81 | + const fn = protect(); |
| 82 | + const original = {}; |
| 83 | + |
| 84 | + expect(fn(original)).to.deep.equal(original); |
| 85 | + }); |
| 86 | + |
| 87 | + testOmit('with hook.result', 'result'); |
| 88 | + testOmit('with hook.dispatch already set', 'dispatch'); |
| 89 | +}); |
0 commit comments