|
| 1 | +const test = require("mocha-sinon-chai"); |
| 2 | + |
| 3 | +const { Origin, sources } = require("../src/Origin"); |
| 4 | + |
| 5 | +test.describe("Origin defaultValue as callback", () => { |
| 6 | + let sandbox; |
| 7 | + |
| 8 | + test.beforeEach(() => { |
| 9 | + sandbox = test.sinon.createSandbox(); |
| 10 | + }); |
| 11 | + |
| 12 | + test.afterEach(() => { |
| 13 | + sandbox.restore(); |
| 14 | + sources.clear(); |
| 15 | + }); |
| 16 | + |
| 17 | + test.describe("when Origin has defaultValue callback defined", () => { |
| 18 | + test.describe("read method", () => { |
| 19 | + test.describe("without query", () => { |
| 20 | + test.it( |
| 21 | + "should return the result of defaultValue callback until real value is returned", |
| 22 | + () => { |
| 23 | + const TestOrigin = class extends Origin { |
| 24 | + constructor(id, defaultValue, options) { |
| 25 | + const getDefaultValue = () => { |
| 26 | + return defaultValue + 2; |
| 27 | + }; |
| 28 | + super(id, getDefaultValue, options); |
| 29 | + } |
| 30 | + |
| 31 | + _read() { |
| 32 | + return Promise.resolve(5); |
| 33 | + } |
| 34 | + }; |
| 35 | + const testOrigin = new TestOrigin("", 4); |
| 36 | + test.expect(testOrigin.read.value).to.equal(6); |
| 37 | + return testOrigin.read().then(() => { |
| 38 | + return test.expect(testOrigin.read.value).to.equal(5); |
| 39 | + }); |
| 40 | + } |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + test.describe("with query", () => { |
| 45 | + const QUERY = "foo-query"; |
| 46 | + const TestOrigin = class extends Origin { |
| 47 | + constructor(id, defaultValue, options) { |
| 48 | + const getDefaultValue = query => { |
| 49 | + return query; |
| 50 | + }; |
| 51 | + super(id, getDefaultValue, options); |
| 52 | + } |
| 53 | + |
| 54 | + _read() { |
| 55 | + return Promise.resolve("foo-result"); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + test.describe("with simple query", () => { |
| 60 | + test.it( |
| 61 | + "should pass query to defaultValue callback, and return the result until real value is returned", |
| 62 | + () => { |
| 63 | + const testOrigin = new TestOrigin("", 4).query(QUERY); |
| 64 | + test.expect(testOrigin.read.value).to.equal("foo-query"); |
| 65 | + return testOrigin.read().then(() => { |
| 66 | + return test.expect(testOrigin.read.value).to.equal("foo-result"); |
| 67 | + }); |
| 68 | + } |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + test.describe("with chained query", () => { |
| 73 | + test.it( |
| 74 | + "should pass chained query to defaultValue callback, and return the result until real value is returned", |
| 75 | + () => { |
| 76 | + const testOrigin = new TestOrigin("", 4) |
| 77 | + .query({ |
| 78 | + foo: "foo" |
| 79 | + }) |
| 80 | + .query({ |
| 81 | + foo2: "foo2" |
| 82 | + }); |
| 83 | + test.expect(testOrigin.read.value).to.deep.equal({ |
| 84 | + foo: "foo", |
| 85 | + foo2: "foo2" |
| 86 | + }); |
| 87 | + return testOrigin.read().then(() => { |
| 88 | + return test.expect(testOrigin.read.value).to.equal("foo-result"); |
| 89 | + }); |
| 90 | + } |
| 91 | + ); |
| 92 | + }); |
| 93 | + }); |
| 94 | + }); |
| 95 | + }); |
| 96 | +}); |
0 commit comments