({ method, hooks: hookNames, optionPos, execute, getModel })
| 139 | describe('Model Hook integration', () => { |
| 140 | |
| 141 | function testHooks({ method, hooks: hookNames, optionPos, execute, getModel }) { |
| 142 | it(`passes the transaction to hooks {${hookNames.join(',')}} when calling ${method}`, async function() { |
| 143 | await this.sequelize.transaction(async transaction => { |
| 144 | const hooks = Object.create(null); |
| 145 | |
| 146 | for (const hookName of hookNames) { |
| 147 | hooks[hookName] = sinon.spy(); |
| 148 | } |
| 149 | |
| 150 | const User = Reflect.apply(getModel, this, []); |
| 151 | |
| 152 | for (const [hookName, spy] of Object.entries(hooks)) { |
| 153 | User[hookName](spy); |
| 154 | } |
| 155 | |
| 156 | await Reflect.apply(execute, this, [User]); |
| 157 | |
| 158 | const spyMatcher = []; |
| 159 | // ignore all arguments until we get to the option bag. |
| 160 | for (let i = 0; i < optionPos; i++) { |
| 161 | spyMatcher.push(sinon.match.any); |
| 162 | } |
| 163 | |
| 164 | // find the transaction in the option bag |
| 165 | spyMatcher.push(sinon.match.has('transaction', transaction)); |
| 166 | |
| 167 | for (const [hookName, spy] of Object.entries(hooks)) { |
| 168 | expect( |
| 169 | spy, |
| 170 | `hook ${hookName} did not receive the transaction from CLS.` |
| 171 | ).to.have.been.calledWith(...spyMatcher); |
| 172 | } |
| 173 | }); |
| 174 | }); |
| 175 | } |
| 176 | |
| 177 | testHooks({ |
| 178 | method: 'Model.bulkCreate', |
no test coverage detected