-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: two bugs which affect batchMode
- Loading branch information
Showing
4 changed files
with
244 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { HookTriggerOptions } from "../../src"; | ||
import { trigger } from "../../src"; | ||
import { MemoryService } from "@feathersjs/memory"; | ||
import type { HookContext } from "@feathersjs/feathers"; | ||
import { feathers } from "@feathersjs/feathers"; | ||
import type { MethodName } from "../../src/types.internal"; | ||
|
||
export function mock( | ||
hookNames: MethodName | MethodName[], | ||
options: HookTriggerOptions, | ||
beforeHook?: (context: HookContext) => Promise<HookContext>, | ||
afterHook?: (context: HookContext) => Promise<HookContext>, | ||
) { | ||
hookNames = Array.isArray(hookNames) ? hookNames : [hookNames]; | ||
const app = feathers(); | ||
app.use("/tests", new MemoryService({ multi: true })); | ||
const service = app.service("tests"); | ||
const hook = trigger(options); | ||
|
||
const beforeAll = [hook]; | ||
if (beforeHook) { | ||
beforeAll.push(beforeHook); | ||
} | ||
|
||
const afterAll = [hook]; | ||
if (afterHook) { | ||
afterAll.push(afterHook); | ||
} | ||
|
||
const hooks = { | ||
before: {}, | ||
after: {}, | ||
}; | ||
|
||
hookNames.forEach((hookName) => { | ||
hooks.before[hookName] = beforeAll; | ||
hooks.after[hookName] = afterAll; | ||
}); | ||
|
||
service.hooks(hooks); | ||
|
||
return { | ||
app, | ||
service, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import { mock } from "./base-mock"; | ||
|
||
describe("trigger batch mode", () => { | ||
it("create: triggers on multi create without condition in batch mode", async function () { | ||
let cbCount = 0; | ||
let changeCount = 0; | ||
const { service } = mock("create", { | ||
method: "create", | ||
service: "tests", | ||
batchMode: true, | ||
action: (changes) => { | ||
cbCount++; | ||
changeCount = changes.length; | ||
}, | ||
}); | ||
|
||
await service.create([ | ||
{ id: 0, test: true }, | ||
{ id: 1, test: true }, | ||
{ id: 2, test: true }, | ||
]); | ||
assert.strictEqual(cbCount, 1, "action cb was called only a single time"); | ||
assert.strictEqual( | ||
changeCount, | ||
3, | ||
"action cb was called with three change tuples", | ||
); | ||
}); | ||
|
||
it("patch: triggers on multi create with conditions in batch mode", async function () { | ||
let cbCount = 0; | ||
let changeCount = 0; | ||
const { service } = mock(["create", "patch"], { | ||
service: "tests", | ||
batchMode: true, | ||
conditionsBefore: { | ||
test: true, | ||
}, | ||
conditionsData: { | ||
test: false, | ||
}, | ||
conditionsResult: { | ||
test: false, | ||
}, | ||
action: (changes) => { | ||
cbCount++; | ||
changeCount = changes.length; | ||
}, | ||
}); | ||
|
||
await service.create([ | ||
{ id: 0, test: true }, | ||
{ id: 1, test: true }, | ||
{ id: 2, test: true }, | ||
]); | ||
assert.strictEqual(cbCount, 0, "action cb was not called"); | ||
|
||
await service.patch(null, { | ||
test: false, | ||
}); | ||
|
||
assert.strictEqual(cbCount, 1, "action cb was called only a single time"); | ||
assert.strictEqual( | ||
changeCount, | ||
3, | ||
"action cb was called with three change tuples", | ||
); | ||
|
||
await service.patch(null, { | ||
test: false, | ||
}); | ||
|
||
assert.strictEqual(cbCount, 1, "action cb was called only a single time"); | ||
assert.strictEqual( | ||
changeCount, | ||
3, | ||
"action cb was called with three change tuples", | ||
); | ||
}); | ||
|
||
it("patch: triggers on multi create with complex conditions in batch mode", async function () { | ||
let cbCount = 0; | ||
let changeCount = 0; | ||
const { service } = mock(["create", "patch"], { | ||
service: "tests", | ||
batchMode: true, | ||
conditionsBefore: { | ||
submittedAt: { | ||
$ne: null, | ||
}, | ||
approvedAt: null, | ||
}, | ||
conditionsData: { | ||
approvedAt: { | ||
$ne: null, | ||
}, | ||
}, | ||
conditionsResult: { | ||
approvedAt: { | ||
$ne: null, | ||
}, | ||
declinedAt: null, | ||
}, | ||
action: (changes) => { | ||
cbCount++; | ||
changeCount = changes.length; | ||
}, | ||
}); | ||
|
||
await service.create([ | ||
{ id: 0, submittedAt: null, approvedAt: null, declinedAt: null }, | ||
{ id: 1, submittedAt: null, approvedAt: null, declinedAt: null }, | ||
{ id: 2, submittedAt: null, approvedAt: null, declinedAt: null }, | ||
{ id: 12, submittedAt: null, approvedAt: null, declinedAt: null }, | ||
{ id: 13, submittedAt: null, approvedAt: null, declinedAt: null }, | ||
{ id: 14, submittedAt: null, approvedAt: null, declinedAt: null }, | ||
]); | ||
|
||
await service.patch( | ||
null, | ||
{ | ||
submittedAt: new Date(), | ||
}, | ||
{ | ||
query: { | ||
id: { | ||
$in: [12, 13, 14], | ||
}, | ||
}, | ||
}, | ||
); | ||
|
||
assert.strictEqual(cbCount, 0, "action cb was not called"); | ||
|
||
await service.patch( | ||
null, | ||
{ | ||
approvedAt: new Date(), | ||
}, | ||
{ | ||
query: { | ||
id: { | ||
$in: [12, 13, 14], | ||
}, | ||
}, | ||
}, | ||
); | ||
|
||
assert.strictEqual(cbCount, 1, "action cb was called only a single time"); | ||
assert.strictEqual( | ||
changeCount, | ||
3, | ||
"action cb was called with three change tuples", | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters