|
| 1 | +jest.mock("keystone", () => { |
| 2 | + const list = { |
| 3 | + add: jest.fn(), |
| 4 | + schema: { |
| 5 | + pre: jest.fn() |
| 6 | + }, |
| 7 | + relationship: jest.fn(), |
| 8 | + register: jest.fn() |
| 9 | + }; |
| 10 | + |
| 11 | + return { |
| 12 | + // Use a normal func and not an arrow so it can be used as a constructor |
| 13 | + List: function() { |
| 14 | + return list; |
| 15 | + }, |
| 16 | + Field: { |
| 17 | + Types: {} |
| 18 | + } |
| 19 | + }; |
| 20 | +}); |
| 21 | + |
| 22 | +describe("Weekly", () => { |
| 23 | + let keystone, pre; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + jest.resetModules(); |
| 27 | + require("../../models/Weekly"); |
| 28 | + keystone = require("keystone"); |
| 29 | + |
| 30 | + pre = keystone.List("Weekly").schema.pre; |
| 31 | + }); |
| 32 | + |
| 33 | + it("should set up a pre-validate function", () => { |
| 34 | + expect(pre).toHaveBeenCalledWith("validate", expect.any(Function)); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should call next with no errors when validation succeeds", () => { |
| 38 | + const validatorFn = pre.mock.calls[0][1]; |
| 39 | + |
| 40 | + const next = jest.fn(); |
| 41 | + validatorFn.call({}, next); |
| 42 | + |
| 43 | + expect(next).toHaveBeenCalledWith(); |
| 44 | + }); |
| 45 | + |
| 46 | + it.each` |
| 47 | + field | filledIn | emptyFields |
| 48 | + ${"commentaryTitle"} | ${"title"} | ${"summary and body"} |
| 49 | + ${"commentarySummary"} | ${"summary"} | ${"title and body"} |
| 50 | + ${"commentaryBody"} | ${"body"} | ${"title and summary"} |
| 51 | + `( |
| 52 | + "should validate commentary $emptyFields when $filledIn is set", |
| 53 | + ({ field, filledIn, emptyFields }) => { |
| 54 | + const pre = keystone.List("Weekly").schema.pre; |
| 55 | + |
| 56 | + expect(pre).toHaveBeenCalledWith("validate", expect.any(Function)); |
| 57 | + |
| 58 | + const validatorFn = pre.mock.calls[0][1]; |
| 59 | + |
| 60 | + const next = jest.fn(); |
| 61 | + validatorFn.call( |
| 62 | + { |
| 63 | + [field]: "Test content" |
| 64 | + }, |
| 65 | + next |
| 66 | + ); |
| 67 | + |
| 68 | + const errorMessage = `Commentary ${emptyFields} are both required if commentary ${filledIn} is set`; |
| 69 | + |
| 70 | + expect(next).toHaveBeenCalledWith(Error(errorMessage)); |
| 71 | + } |
| 72 | + ); |
| 73 | +}); |
0 commit comments