|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +describe("$animateCss", function() { |
| 4 | + |
| 5 | + var triggerRAF, element; |
| 6 | + beforeEach(inject(function($$rAF, $rootElement, $document) { |
| 7 | + triggerRAF = function() { |
| 8 | + $$rAF.flush(); |
| 9 | + }; |
| 10 | + |
| 11 | + var body = jqLite($document[0].body); |
| 12 | + element = jqLite('<div></div>'); |
| 13 | + $rootElement.append(element); |
| 14 | + body.append($rootElement); |
| 15 | + })); |
| 16 | + |
| 17 | + describe("without animation", function() { |
| 18 | + |
| 19 | + it("should apply the provided [from] CSS to the element", inject(function($animateCss) { |
| 20 | + $animateCss(element, { from: { height: '50px' }}).start(); |
| 21 | + expect(element.css('height')).toBe('50px'); |
| 22 | + })); |
| 23 | + |
| 24 | + it("should apply the provided [to] CSS to the element after the first frame", inject(function($animateCss) { |
| 25 | + $animateCss(element, { to: { width: '50px' }}).start(); |
| 26 | + expect(element.css('width')).not.toBe('50px'); |
| 27 | + triggerRAF(); |
| 28 | + expect(element.css('width')).toBe('50px'); |
| 29 | + })); |
| 30 | + |
| 31 | + it("should apply the provided [addClass] CSS classes to the element after the first frame", inject(function($animateCss) { |
| 32 | + $animateCss(element, { addClass: 'golden man' }).start(); |
| 33 | + expect(element).not.toHaveClass('golden man'); |
| 34 | + triggerRAF(); |
| 35 | + expect(element).toHaveClass('golden man'); |
| 36 | + })); |
| 37 | + |
| 38 | + it("should apply the provided [removeClass] CSS classes to the element after the first frame", inject(function($animateCss) { |
| 39 | + element.addClass('silver'); |
| 40 | + $animateCss(element, { removeClass: 'silver dude' }).start(); |
| 41 | + expect(element).toHaveClass('silver'); |
| 42 | + triggerRAF(); |
| 43 | + expect(element).not.toHaveClass('silver'); |
| 44 | + })); |
| 45 | + |
| 46 | + it("should return an animator with a start method which returns a promise", inject(function($animateCss) { |
| 47 | + var promise = $animateCss(element, { addClass: 'cool' }).start(); |
| 48 | + expect(isPromiseLike(promise)).toBe(true); |
| 49 | + })); |
| 50 | + |
| 51 | + it("should return an animator with an end method which returns a promise", inject(function($animateCss) { |
| 52 | + var promise = $animateCss(element, { addClass: 'cool' }).end(); |
| 53 | + expect(isPromiseLike(promise)).toBe(true); |
| 54 | + })); |
| 55 | + |
| 56 | + it("should only resolve the promise once both a digest and RAF have passed after start", |
| 57 | + inject(function($animateCss, $rootScope) { |
| 58 | + |
| 59 | + var doneSpy = jasmine.createSpy(); |
| 60 | + var runner = $animateCss(element, { addClass: 'cool' }).start(); |
| 61 | + |
| 62 | + runner.then(doneSpy); |
| 63 | + expect(doneSpy).not.toHaveBeenCalled(); |
| 64 | + |
| 65 | + triggerRAF(); |
| 66 | + expect(doneSpy).not.toHaveBeenCalled(); |
| 67 | + |
| 68 | + $rootScope.$digest(); |
| 69 | + expect(doneSpy).toHaveBeenCalled(); |
| 70 | + })); |
| 71 | + |
| 72 | + it("should resolve immediately if runner.end() is called", |
| 73 | + inject(function($animateCss, $rootScope) { |
| 74 | + |
| 75 | + var doneSpy = jasmine.createSpy(); |
| 76 | + var runner = $animateCss(element, { addClass: 'cool' }).start(); |
| 77 | + |
| 78 | + runner.then(doneSpy); |
| 79 | + runner.end(); |
| 80 | + expect(doneSpy).not.toHaveBeenCalled(); |
| 81 | + |
| 82 | + $rootScope.$digest(); |
| 83 | + expect(doneSpy).toHaveBeenCalled(); |
| 84 | + })); |
| 85 | + |
| 86 | + it("should reject immediately if runner.end() is called", |
| 87 | + inject(function($animateCss, $rootScope) { |
| 88 | + |
| 89 | + var cancelSpy = jasmine.createSpy(); |
| 90 | + var runner = $animateCss(element, { addClass: 'cool' }).start(); |
| 91 | + |
| 92 | + runner.catch(cancelSpy); |
| 93 | + runner.cancel(); |
| 94 | + expect(cancelSpy).not.toHaveBeenCalled(); |
| 95 | + |
| 96 | + $rootScope.$digest(); |
| 97 | + expect(cancelSpy).toHaveBeenCalled(); |
| 98 | + })); |
| 99 | + |
| 100 | + it("should not resolve after the next frame if the runner has already been cancelled", |
| 101 | + inject(function($animateCss, $rootScope) { |
| 102 | + |
| 103 | + var doneSpy = jasmine.createSpy(); |
| 104 | + var cancelSpy = jasmine.createSpy(); |
| 105 | + var runner = $animateCss(element, { addClass: 'cool' }).start(); |
| 106 | + |
| 107 | + runner.then(doneSpy, cancelSpy); |
| 108 | + runner.cancel(); |
| 109 | + |
| 110 | + $rootScope.$digest(); |
| 111 | + expect(cancelSpy).toHaveBeenCalled(); |
| 112 | + expect(doneSpy).not.toHaveBeenCalled(); |
| 113 | + |
| 114 | + triggerRAF(); |
| 115 | + expect(cancelSpy).toHaveBeenCalled(); |
| 116 | + expect(doneSpy).not.toHaveBeenCalled(); |
| 117 | + })); |
| 118 | + }); |
| 119 | + |
| 120 | +}); |
0 commit comments