[Imagery] Test mct-background-image filters

This commit is contained in:
Victor Woeltjen 2016-11-15 13:50:28 -08:00
parent 2f5dc8a887
commit 2dfde9a612

View File

@ -35,7 +35,10 @@ define(
mockDocument = [ mockDocument = [
jasmine.createSpyObj('document', ['createElement']) jasmine.createSpyObj('document', ['createElement'])
]; ];
mockScope = jasmine.createSpyObj('scope', ['$watch']); mockScope = jasmine.createSpyObj('scope', [
'$watch',
'$watchCollection'
]);
mockElement = jasmine.createSpyObj('element', ['css']); mockElement = jasmine.createSpyObj('element', ['css']);
testImage = {}; testImage = {};
@ -52,46 +55,70 @@ define(
expect(directive.scope.mctBackgroundImage).toEqual("="); expect(directive.scope.mctBackgroundImage).toEqual("=");
}); });
it("watches for changes to the URL", function () { describe("once linked", function () {
directive.link(mockScope, mockElement, {}); beforeEach(function () {
expect(mockScope.$watch).toHaveBeenCalledWith( directive.link(mockScope, mockElement, {});
'mctBackgroundImage', });
jasmine.any(Function)
);
});
it("updates images in-order, even when they load out-of-order", function () { it("watches for changes to the URL", function () {
var firstOnload; expect(mockScope.$watch).toHaveBeenCalledWith(
'mctBackgroundImage',
jasmine.any(Function)
);
});
directive.link(mockScope, mockElement); it("updates images in-order, even when they load out-of-order", function () {
var firstOnload;
mockScope.$watch.mostRecentCall.args[1]("some/url/0"); mockScope.$watch.mostRecentCall.args[1]("some/url/0");
firstOnload = testImage.onload; firstOnload = testImage.onload;
mockScope.$watch.mostRecentCall.args[1]("some/url/1"); mockScope.$watch.mostRecentCall.args[1]("some/url/1");
// Resolve in a different order // Resolve in a different order
testImage.onload(); testImage.onload();
firstOnload(); firstOnload();
// Should still have taken the more recent value // Should still have taken the more recent value
expect(mockElement.css.mostRecentCall.args).toEqual([ expect(mockElement.css.mostRecentCall.args).toEqual([
"background-image", "background-image",
"url('some/url/1')" "url('some/url/1')"
]); ]);
}); });
it("clears the background image when undefined is passed in", function () { it("clears the background image when undefined is passed in", function () {
directive.link(mockScope, mockElement); mockScope.$watch.mostRecentCall.args[1]("some/url/0");
testImage.onload();
mockScope.$watch.mostRecentCall.args[1](undefined);
mockScope.$watch.mostRecentCall.args[1]("some/url/0"); expect(mockElement.css.mostRecentCall.args).toEqual([
testImage.onload(); "background-image",
mockScope.$watch.mostRecentCall.args[1](undefined); "none"
]);
});
expect(mockElement.css.mostRecentCall.args).toEqual([ it("updates filters on change", function () {
"background-image", var filters = { brightness: 123, contrast: 21 };
"none" mockScope.$watchCollection.calls.forEach(function (call) {
]); if (call.args[0] === 'filters') {
call.args[1](filters);
}
});
expect(mockElement.css).toHaveBeenCalledWith(
'filter',
'brightness(123%) contrast(21%)'
);
});
it("clears filters when none are present", function () {
mockScope.$watchCollection.calls.forEach(function (call) {
if (call.args[0] === 'filters') {
call.args[1](undefined);
}
});
expect(mockElement.css)
.toHaveBeenCalledWith('filter', '');
});
}); });
}); });
} }