material-motion-testing-utils
Version:
Utilities for testing the Material Motion framework
51 lines • 2.18 kB
JavaScript
;
/** @license
* Copyright 2016 - present The Material Motion Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const mocha_sugar_free_1 = require("mocha-sugar-free");
const sinon_1 = require("sinon");
const createMockRAF = require("mock-raf");
/**
* Replaces `window.requestAnimationFrame`, `performance.now`, and `Date.now`
* with mocks for the duration of a mocha testing suite.
*
* The two `now` methods will only be incremented when `mockRAF.step` is called.
*/
function useMockedRAF(closure) {
return () => {
const mockRAF = createMockRAF();
const initialTime = Date.now();
function mockNow() {
return initialTime + mockRAF.now();
}
mocha_sugar_free_1.before(() => {
sinon_1.stub(window, 'requestAnimationFrame').callsFake(mockRAF.raf);
sinon_1.stub(window, 'cancelAnimationFrame').callsFake(mockRAF.cancel);
sinon_1.stub(Date, 'now').callsFake(mockNow);
sinon_1.stub(performance, 'now').callsFake(mockRAF.now);
});
mocha_sugar_free_1.after(() => {
window.requestAnimationFrame.restore(); // tslint:disable-line: no-unbound-method
window.cancelAnimationFrame.restore(); // tslint:disable-line: no-unbound-method
Date.now.restore(); // tslint:disable-line: no-unbound-method
performance.now.restore(); // tslint:disable-line: no-unbound-method
});
return closure(mockRAF);
};
}
exports.default = useMockedRAF;
;
//# sourceMappingURL=useMockedRAF.js.map