From 267053b431e10990acaf6c236d2e53a7d09a5548 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Tue, 24 Mar 2015 17:01:09 -0700 Subject: [PATCH] [Core] Add test cases for CachingModelDecorator WTD-1033. --- .../test/models/CachingModelDecoratorSpec.js | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/platform/core/test/models/CachingModelDecoratorSpec.js b/platform/core/test/models/CachingModelDecoratorSpec.js index 3943cf3055..4718cc94b6 100644 --- a/platform/core/test/models/CachingModelDecoratorSpec.js +++ b/platform/core/test/models/CachingModelDecoratorSpec.js @@ -6,6 +6,55 @@ define( "use strict"; describe("The caching model decorator", function () { + var mockModelService, + mockCallback, + testModels, + decorator; + + function asPromise(value) { + return (value || {}).then ? value : { + then: function (callback) { + return asPromise(callback(value)); + } + }; + } + + beforeEach(function () { + mockCallback = jasmine.createSpy(); + mockModelService = jasmine.createSpyObj('modelService', ['getModels']); + testModels = { + a: { someKey: "some value" }, + b: { someOtherKey: "some other value" } + }; + mockModelService.getModels.andReturn(asPromise(testModels)); + decorator = new CachingModelDecorator(mockModelService); + }); + + it("loads models from its wrapped model service", function () { + decorator.getModels(['a', 'b']).then(mockCallback); + expect(mockCallback).toHaveBeenCalledWith(testModels); + }); + + it("does not try to reload cached models", function () { + mockModelService.getModels.andReturn(asPromise({ a: testModels.a })); + decorator.getModels(['a']); + mockModelService.getModels.andReturn(asPromise(testModels)); + decorator.getModels(['a', 'b']); + expect(mockModelService.getModels).not.toHaveBeenCalledWith(['a', 'b']); + expect(mockModelService.getModels.mostRecentCall.args[0]).toEqual(['b']); + }); + + it("does not call its wrapped model service if not needed", function () { + decorator.getModels(['a', 'b']); + expect(mockModelService.getModels.calls.length).toEqual(1); + decorator.getModels(['a', 'b']).then(mockCallback); + expect(mockModelService.getModels.calls.length).toEqual(1); + // Verify that we still got back our models, even though + // no new call to the wrapped service was made + expect(mockCallback).toHaveBeenCalledWith(testModels); + }); + + }); } ); \ No newline at end of file