|
| 1 | +const dataPoint = require("./DataPoint"); |
| 2 | +const { Accumulator } = require("./Accumulator"); |
| 3 | +const { Cache } = require("./Cache"); |
| 4 | + |
| 5 | +const sayHello = value => `Hello ${value}`; |
| 6 | +const getAccumulator = (input, acc) => acc; |
| 7 | + |
| 8 | +const shallowTracer = { |
| 9 | + startSpan: () => Object.create(shallowTracer), |
| 10 | + setTag: () => true, |
| 11 | + log: () => true |
| 12 | +}; |
| 13 | + |
| 14 | +describe("resolveFromAccumulator", () => { |
| 15 | + it("should run reducers using accumulator as input", async () => { |
| 16 | + const acc = new Accumulator({ value: "world" }); |
| 17 | + const result = await dataPoint.resolveFromAccumulator(acc, sayHello); |
| 18 | + expect(result).toEqual("Hello world"); |
| 19 | + }); |
| 20 | +}); |
| 21 | + |
| 22 | +describe("resolveFromInput", () => { |
| 23 | + it("should run reducers against input", async () => { |
| 24 | + const result = await dataPoint.resolveFromInput("world", sayHello); |
| 25 | + expect(result).toEqual("Hello world"); |
| 26 | + }); |
| 27 | + |
| 28 | + describe("passing options object to accumulator", () => { |
| 29 | + const options = { |
| 30 | + locals: { |
| 31 | + key: "value" |
| 32 | + }, |
| 33 | + cache: { |
| 34 | + get: () => true, |
| 35 | + set: () => true |
| 36 | + }, |
| 37 | + tracer: "tracer" |
| 38 | + }; |
| 39 | + it("should pass locals object", async () => { |
| 40 | + const result = await dataPoint.resolveFromInput( |
| 41 | + "input", |
| 42 | + getAccumulator, |
| 43 | + options |
| 44 | + ); |
| 45 | + expect(result).toHaveProperty("locals", options.locals); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should pass cache object", async () => { |
| 49 | + const result = await dataPoint.resolveFromInput( |
| 50 | + "input", |
| 51 | + getAccumulator, |
| 52 | + options |
| 53 | + ); |
| 54 | + expect(result).toHaveProperty("cache", options.cache); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should pass tracer object", async () => { |
| 58 | + const result = await dataPoint.resolveFromInput( |
| 59 | + "input", |
| 60 | + getAccumulator, |
| 61 | + options |
| 62 | + ); |
| 63 | + expect(result).toHaveProperty("tracer", options.tracer); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should pass resolveFromAccumulator method", async () => { |
| 67 | + const nestedResolver = (input, acc) => `nested ${acc.value}`; |
| 68 | + |
| 69 | + const resolveNestedReducer = (input, acc) => { |
| 70 | + return acc.resolve(nestedResolver); |
| 71 | + }; |
| 72 | + |
| 73 | + const result = await dataPoint.resolveFromInput( |
| 74 | + "input", |
| 75 | + resolveNestedReducer, |
| 76 | + options |
| 77 | + ); |
| 78 | + expect(result).toEqual("nested input"); |
| 79 | + }); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe("validateLocals", () => { |
| 84 | + it("should only allow undefined or plain object", () => { |
| 85 | + expect(() => { |
| 86 | + dataPoint.validateLocals(); |
| 87 | + }).not.toThrow(); |
| 88 | + |
| 89 | + expect(() => { |
| 90 | + dataPoint.validateLocals({}); |
| 91 | + }).not.toThrow(); |
| 92 | + }); |
| 93 | + |
| 94 | + it("should throw error on any un-valid value", () => { |
| 95 | + expect(() => { |
| 96 | + dataPoint.validateLocals(null); |
| 97 | + }).toThrowErrorMatchingInlineSnapshot( |
| 98 | + `"'options.locals' must be undefined or an object"` |
| 99 | + ); |
| 100 | + |
| 101 | + expect(() => { |
| 102 | + dataPoint.validateLocals("string"); |
| 103 | + }).toThrowErrorMatchingInlineSnapshot( |
| 104 | + `"'options.locals' must be undefined or an object"` |
| 105 | + ); |
| 106 | + |
| 107 | + expect(() => { |
| 108 | + dataPoint.validateLocals([]); |
| 109 | + }).toThrowErrorMatchingInlineSnapshot( |
| 110 | + `"'options.locals' must be undefined or an object"` |
| 111 | + ); |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +describe("validateTracer", () => { |
| 116 | + it("should only allow undefined or well defined tracer span API", () => { |
| 117 | + expect(() => { |
| 118 | + dataPoint.validateTracer(); |
| 119 | + }).not.toThrow(); |
| 120 | + |
| 121 | + expect(() => { |
| 122 | + dataPoint.validateTracer(shallowTracer); |
| 123 | + }).not.toThrow(); |
| 124 | + }); |
| 125 | + |
| 126 | + it("should throw error on any un-valid value", () => { |
| 127 | + expect(() => { |
| 128 | + dataPoint.validateTracer({}); |
| 129 | + }).toThrowErrorMatchingInlineSnapshot( |
| 130 | + `"tracer.startSpan must be a function, tracer expects opentracing API (see https://opentracing.io)"` |
| 131 | + ); |
| 132 | + |
| 133 | + expect(() => { |
| 134 | + dataPoint.validateTracer({ |
| 135 | + startSpan: () => true |
| 136 | + }); |
| 137 | + }).toThrowErrorMatchingInlineSnapshot( |
| 138 | + `"tracer.setTag must be a function, tracer expects opentracing API (see https://opentracing.io)"` |
| 139 | + ); |
| 140 | + |
| 141 | + expect(() => { |
| 142 | + dataPoint.validateTracer({ |
| 143 | + startSpan: () => true, |
| 144 | + setTag: () => true |
| 145 | + }); |
| 146 | + }).toThrowErrorMatchingInlineSnapshot( |
| 147 | + `"tracer.log must be a function, tracer expects opentracing API (see https://opentracing.io)"` |
| 148 | + ); |
| 149 | + }); |
| 150 | +}); |
| 151 | + |
| 152 | +describe("DataPoint", () => { |
| 153 | + const DataPoint = dataPoint.DataPoint; |
| 154 | + |
| 155 | + describe("constructor", () => { |
| 156 | + it("should initialize cache", () => { |
| 157 | + const dp = new DataPoint(); |
| 158 | + expect(dp.cache).toBeInstanceOf(Cache); |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + describe("create", () => { |
| 163 | + it("should create instance of itself", () => { |
| 164 | + const dp = DataPoint.create(); |
| 165 | + expect(dp).toBeInstanceOf(DataPoint); |
| 166 | + }); |
| 167 | + }); |
| 168 | + |
| 169 | + describe("resolve", () => { |
| 170 | + it("should resolve input", async () => { |
| 171 | + const dp = new DataPoint(); |
| 172 | + const result = await dp.resolve("world", sayHello); |
| 173 | + expect(result).toEqual("Hello world"); |
| 174 | + }); |
| 175 | + |
| 176 | + it("should pass cache object", async () => { |
| 177 | + const dp = new DataPoint(); |
| 178 | + dp.cache.get = () => true; |
| 179 | + dp.cache.set = () => true; |
| 180 | + |
| 181 | + const result = await dp.resolve("input", getAccumulator); |
| 182 | + expect(result).toHaveProperty("cache", dp.cache); |
| 183 | + }); |
| 184 | + |
| 185 | + it("should pass options object", async () => { |
| 186 | + const dp = new DataPoint(); |
| 187 | + const options = { |
| 188 | + locals: { |
| 189 | + key: "value" |
| 190 | + }, |
| 191 | + tracer: shallowTracer |
| 192 | + }; |
| 193 | + const result = await dp.resolve("input", getAccumulator, options); |
| 194 | + expect(result).toHaveProperty("locals", options.locals); |
| 195 | + expect(result).toHaveProperty("tracer"); |
| 196 | + }); |
| 197 | + }); |
| 198 | +}); |
0 commit comments