[Performance] Update specs
Add tests to verify that directives stop polling after their scope is destroyed (to prevent resource leaks); those changes address resource leaks identified in the context of WTD-717.
This commit is contained in:
@@ -14,7 +14,7 @@ define(
|
|||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
mockTimeout = jasmine.createSpy("$timeout");
|
mockTimeout = jasmine.createSpy("$timeout");
|
||||||
mockScope = jasmine.createSpyObj("$scope", ["$eval"]);
|
mockScope = jasmine.createSpyObj("$scope", ["$eval", "$on"]);
|
||||||
|
|
||||||
testElement = { offsetWidth: 100, offsetHeight: 200 };
|
testElement = { offsetWidth: 100, offsetHeight: 200 };
|
||||||
testAttrs = { mctResize: "some-expr" };
|
testAttrs = { mctResize: "some-expr" };
|
||||||
@@ -63,6 +63,32 @@ define(
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("stops size checking for size changes after destroy", function () {
|
||||||
|
mctResize.link(mockScope, [testElement], testAttrs);
|
||||||
|
|
||||||
|
// First, make sure there's a $destroy observer
|
||||||
|
expect(mockScope.$on)
|
||||||
|
.toHaveBeenCalledWith("$destroy", jasmine.any(Function));
|
||||||
|
|
||||||
|
// Should have scheduled the first timeout
|
||||||
|
expect(mockTimeout.calls.length).toEqual(1);
|
||||||
|
|
||||||
|
// Fire the timeout
|
||||||
|
mockTimeout.mostRecentCall.args[0]();
|
||||||
|
|
||||||
|
// Should have scheduled another timeout
|
||||||
|
expect(mockTimeout.calls.length).toEqual(2);
|
||||||
|
|
||||||
|
// Broadcast a destroy event
|
||||||
|
mockScope.$on.mostRecentCall.args[1]();
|
||||||
|
|
||||||
|
// Fire the timeout
|
||||||
|
mockTimeout.mostRecentCall.args[0]();
|
||||||
|
|
||||||
|
// Should NOT have scheduled another timeout
|
||||||
|
expect(mockTimeout.calls.length).toEqual(2);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -15,6 +15,7 @@ define(
|
|||||||
mockElement,
|
mockElement,
|
||||||
mockCanvas,
|
mockCanvas,
|
||||||
mockGL,
|
mockGL,
|
||||||
|
mockCancelInterval,
|
||||||
mctChart;
|
mctChart;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
@@ -23,9 +24,10 @@ define(
|
|||||||
mockLog =
|
mockLog =
|
||||||
jasmine.createSpyObj("$log", ["warn", "info", "debug"]);
|
jasmine.createSpyObj("$log", ["warn", "info", "debug"]);
|
||||||
mockScope =
|
mockScope =
|
||||||
jasmine.createSpyObj("$scope", ["$watchCollection"]);
|
jasmine.createSpyObj("$scope", ["$watchCollection", "$on"]);
|
||||||
mockElement =
|
mockElement =
|
||||||
jasmine.createSpyObj("element", ["find"]);
|
jasmine.createSpyObj("element", ["find"]);
|
||||||
|
mockCancelInterval = jasmine.createSpy("cancelInterval");
|
||||||
|
|
||||||
|
|
||||||
// mct-chart uses GLChart, so it needs WebGL API
|
// mct-chart uses GLChart, so it needs WebGL API
|
||||||
@@ -70,6 +72,7 @@ define(
|
|||||||
|
|
||||||
mockElement.find.andReturn([mockCanvas]);
|
mockElement.find.andReturn([mockCanvas]);
|
||||||
mockCanvas.getContext.andReturn(mockGL);
|
mockCanvas.getContext.andReturn(mockGL);
|
||||||
|
mockInterval.andReturn(mockCancelInterval);
|
||||||
|
|
||||||
mctChart = new MCTChart(mockInterval, mockLog);
|
mctChart = new MCTChart(mockInterval, mockLog);
|
||||||
});
|
});
|
||||||
@@ -150,6 +153,26 @@ define(
|
|||||||
expect(mockLog.warn).not.toHaveBeenCalled();
|
expect(mockLog.warn).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Avoid resource leaks
|
||||||
|
it("stops polling for size changes on destroy", function () {
|
||||||
|
mctChart.link(mockScope, mockElement);
|
||||||
|
|
||||||
|
// Should be listening for a destroy event
|
||||||
|
expect(mockScope.$on).toHaveBeenCalledWith(
|
||||||
|
"$destroy",
|
||||||
|
jasmine.any(Function)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Precondition - interval still active
|
||||||
|
expect(mockCancelInterval).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
// Broadcast a $destroy
|
||||||
|
mockScope.$on.mostRecentCall.args[1]();
|
||||||
|
|
||||||
|
// Should have stopped the interval
|
||||||
|
expect(mockCancelInterval).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
Reference in New Issue
Block a user