|
| 1 | +/** |
| 2 | +* Copyright (c) 2015-present, Parse, LLC. |
| 3 | +* All rights reserved. |
| 4 | +* |
| 5 | +* This source code is licensed under the BSD-style license found in the |
| 6 | +* LICENSE file in the root directory of this source tree. An additional grant |
| 7 | +* of patent rights can be found in the PATENTS file in the same directory. |
| 8 | +*/ |
| 9 | + |
| 10 | +jest.dontMock('../ParseHooks'); |
| 11 | +jest.dontMock('../CoreManager'); |
| 12 | +jest.dontMock('../decode'); |
| 13 | +jest.dontMock('../encode'); |
| 14 | +jest.dontMock('../ParsePromise'); |
| 15 | + |
| 16 | +var Hooks = require('../ParseHooks'); |
| 17 | +var CoreManager = require('../CoreManager'); |
| 18 | +var ParsePromise = require('../ParsePromise'); |
| 19 | + |
| 20 | +var defaultController = CoreManager.getHooksController(); |
| 21 | + |
| 22 | +describe('Hooks', () => { |
| 23 | + beforeEach(() => { |
| 24 | + var run = jest.genMockFunction(); |
| 25 | + run.mockReturnValue(ParsePromise.as({ |
| 26 | + result: {} |
| 27 | + })); |
| 28 | + defaultController.sendRequest = run; |
| 29 | + CoreManager.setHooksController(defaultController); |
| 30 | + }); |
| 31 | + |
| 32 | + it('shoud properly build GET functions', () => { |
| 33 | + Hooks.getFunctions(); |
| 34 | + |
| 35 | + expect(CoreManager.getHooksController().sendRequest.mock.calls[0]) |
| 36 | + .toEqual(['GET', '/hooks/functions']); |
| 37 | + }); |
| 38 | + |
| 39 | + it('shoud properly build GET triggers', () => { |
| 40 | + Hooks.getTriggers(); |
| 41 | + |
| 42 | + expect(CoreManager.getHooksController().sendRequest.mock.calls[0]) |
| 43 | + .toEqual(['GET', '/hooks/triggers']); |
| 44 | + }) |
| 45 | + |
| 46 | + it('shoud properly build GET function', () => { |
| 47 | + Hooks.getFunction('functionName'); |
| 48 | + |
| 49 | + expect(CoreManager.getHooksController().sendRequest.mock.calls[0]) |
| 50 | + .toEqual(['GET', '/hooks/functions/functionName']); |
| 51 | + }) |
| 52 | + |
| 53 | + it('shoud properly build GET trigger', () => { |
| 54 | + Hooks.getTrigger('MyClass', 'beforeSave'); |
| 55 | + |
| 56 | + expect(CoreManager.getHooksController().sendRequest.mock.calls[0]) |
| 57 | + .toEqual(['GET', '/hooks/triggers/MyClass/beforeSave']); |
| 58 | + }) |
| 59 | + |
| 60 | + it('shoud properly build POST function', () => { |
| 61 | + Hooks.createFunction('myFunction', 'https://dummy.com'); |
| 62 | + |
| 63 | + expect(CoreManager.getHooksController().sendRequest.mock.calls[0]) |
| 64 | + .toEqual(['POST', '/hooks/functions', { |
| 65 | + functionName: 'myFunction', |
| 66 | + url: 'https://dummy.com' |
| 67 | + }]); |
| 68 | + }) |
| 69 | + |
| 70 | + it('shoud properly build POST trigger', () => { |
| 71 | + Hooks.createTrigger('MyClass', 'beforeSave', 'https://dummy.com'); |
| 72 | + |
| 73 | + expect(CoreManager.getHooksController().sendRequest.mock.calls[0]) |
| 74 | + .toEqual(['POST', '/hooks/triggers', { |
| 75 | + className: 'MyClass', |
| 76 | + triggerName: 'beforeSave', |
| 77 | + url: 'https://dummy.com' |
| 78 | + }]); |
| 79 | + }) |
| 80 | + |
| 81 | + it('shoud properly build PUT function', () => { |
| 82 | + Hooks.updateFunction('myFunction', 'https://dummy.com'); |
| 83 | + |
| 84 | + expect(CoreManager.getHooksController().sendRequest.mock.calls[0]) |
| 85 | + .toEqual(['PUT', '/hooks/functions/myFunction', { |
| 86 | + url: 'https://dummy.com' |
| 87 | + }]); |
| 88 | + }) |
| 89 | + |
| 90 | + it('shoud properly build PUT trigger', () => { |
| 91 | + Hooks.updateTrigger('MyClass', 'beforeSave', 'https://dummy.com'); |
| 92 | + |
| 93 | + expect(CoreManager.getHooksController().sendRequest.mock.calls[0]) |
| 94 | + .toEqual(['PUT', '/hooks/triggers/MyClass/beforeSave', { |
| 95 | + url: 'https://dummy.com' |
| 96 | + }]); |
| 97 | + }) |
| 98 | + |
| 99 | + |
| 100 | + it('shoud properly build removeFunction', () => { |
| 101 | + Hooks.removeFunction('myFunction'); |
| 102 | + |
| 103 | + expect(CoreManager.getHooksController().sendRequest.mock.calls[0]) |
| 104 | + .toEqual(['PUT', '/hooks/functions/myFunction', { "__op": "Delete" }]); |
| 105 | + }) |
| 106 | + |
| 107 | + it('shoud properly build removeTrigger', () => { |
| 108 | + Hooks.removeTrigger('MyClass', 'beforeSave'); |
| 109 | + |
| 110 | + expect(CoreManager.getHooksController().sendRequest.mock.calls[0]) |
| 111 | + .toEqual(['PUT', '/hooks/triggers/MyClass/beforeSave', { "__op": "Delete" }]); |
| 112 | + }) |
| 113 | + |
| 114 | + it('shoud throw invalid create', () => { |
| 115 | + Hooks.create({functionName: 'myFunction'}).then(() => { |
| 116 | + fail('should not succeed') |
| 117 | + }).catch((err) => { |
| 118 | + expect(err.code).toBe(143); |
| 119 | + expect(err.error).toBe('invalid hook declaration'); |
| 120 | + }); |
| 121 | + |
| 122 | + Hooks.create({url: 'http://dummy.com'}).then(() => { |
| 123 | + fail('should not succeed') |
| 124 | + }).catch((err) => { |
| 125 | + expect(err.code).toBe(143); |
| 126 | + expect(err.error).toBe('invalid hook declaration'); |
| 127 | + }); |
| 128 | + |
| 129 | + Hooks.create({className: 'MyClass'}).then(() => { |
| 130 | + fail('should not succeed') |
| 131 | + }).catch((err) => { |
| 132 | + expect(err.code).toBe(143); |
| 133 | + expect(err.error).toBe('invalid hook declaration'); |
| 134 | + }); |
| 135 | + |
| 136 | + Hooks.create({className: 'MyClass', url: 'http://dummy.com'}).then(() => { |
| 137 | + fail('should not succeed') |
| 138 | + }).catch((err) => { |
| 139 | + expect(err.code).toBe(143); |
| 140 | + expect(err.error).toBe('invalid hook declaration'); |
| 141 | + }); |
| 142 | + |
| 143 | + Hooks.create({className: 'MyClass', triggerName: 'beforeSave'}).then(() => { |
| 144 | + fail('should not succeed') |
| 145 | + }).catch((err) => { |
| 146 | + expect(err.code).toBe(143); |
| 147 | + expect(err.error).toBe('invalid hook declaration'); |
| 148 | + }); |
| 149 | + }) |
| 150 | + |
| 151 | + it('shoud throw invalid update', () => { |
| 152 | + Hooks.update({functionssName: 'myFunction'}).then(() => { |
| 153 | + fail('should not succeed') |
| 154 | + }).catch((err) => { |
| 155 | + expect(err.code).toBe(143); |
| 156 | + expect(err.error).toBe('invalid hook declaration'); |
| 157 | + }); |
| 158 | + |
| 159 | + Hooks.update({className: 'MyClass'}).then(() => { |
| 160 | + fail('should not succeed') |
| 161 | + }).catch((err) => { |
| 162 | + expect(err.code).toBe(143); |
| 163 | + expect(err.error).toBe('invalid hook declaration'); |
| 164 | + }); |
| 165 | + |
| 166 | + Hooks.update({className: 'MyClass', url: 'http://dummy.com'}).then(() => { |
| 167 | + fail('should not succeed') |
| 168 | + }).catch((err) => { |
| 169 | + expect(err.code).toBe(143); |
| 170 | + expect(err.error).toBe('invalid hook declaration'); |
| 171 | + }); |
| 172 | + }) |
| 173 | + |
| 174 | + it('shoud throw invalid remove', () => { |
| 175 | + Hooks.remove({functionssName: 'myFunction'}).then(() => { |
| 176 | + fail('should not succeed') |
| 177 | + }).catch((err) => { |
| 178 | + expect(err.code).toBe(143); |
| 179 | + expect(err.error).toBe('invalid hook declaration'); |
| 180 | + }); |
| 181 | + |
| 182 | + Hooks.remove({className: 'MyClass'}).then(() => { |
| 183 | + fail('should not succeed') |
| 184 | + }).catch((err) => { |
| 185 | + expect(err.code).toBe(143); |
| 186 | + expect(err.error).toBe('invalid hook declaration'); |
| 187 | + }); |
| 188 | + |
| 189 | + Hooks.remove({className: 'MyClass', url: 'http://dummy.com'}).then(() => { |
| 190 | + fail('should not succeed') |
| 191 | + }).catch((err) => { |
| 192 | + expect(err.code).toBe(143); |
| 193 | + expect(err.error).toBe('invalid hook declaration'); |
| 194 | + }); |
| 195 | + }) |
| 196 | + |
| 197 | + |
| 198 | +}); |
0 commit comments