Skip to content

Commit

Permalink
[added] useMockedPerformance
Browse files Browse the repository at this point in the history
Summary: Closes #207

Reviewers: O2 Material Motion, O3 Material JavaScript platform reviewers, #material_motion, featherless

Reviewed By: O2 Material Motion, #material_motion, featherless

Tags: #material_motion

Differential Revision: http://codereview.cc/D3108
  • Loading branch information
appsforartists committed Apr 27, 2017
1 parent 8d9606b commit 82c1546
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 50 deletions.
105 changes: 55 additions & 50 deletions packages/core/src/operators/__tests__/velocity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ require('chai').use(
);

import {
Performance,
useMockedPerformance
} from 'material-motion-testing-utils';

import {
Expand All @@ -42,54 +44,57 @@ import {
} from '../../observables/';

describe('motionObservable.velocity',
() => {
let valueStream;
let pulseStream;
let valueSubject;
let pulseSubject;
let listener;

beforeEach(
() => {
valueSubject = new IndefiniteSubject();
pulseSubject = new IndefiniteSubject();
valueStream = MotionObservable.from(valueSubject);
pulseStream = MotionObservable.from(pulseSubject);
listener = stub();
}
);

it(`should not dispatch unless pulse does`,
() => {
valueStream.velocity(pulseStream).subscribe(listener);

valueSubject.next(0);
valueSubject.next(1);
valueSubject.next(2);

expect(listener).not.to.have.been.called;
}
);

it(`should dispatch every time it receives a value is pulse isn't supplied`,
() => {
valueStream.velocity().subscribe(listener);

valueSubject.next(0);
valueSubject.next(1);

expect(listener).to.have.been.calledTwice;
}
);

it(`should dispatch 0 if pulse dispatches before it's received values from upstream`,
() => {
valueStream.velocity(pulseStream).subscribe(listener);

pulseSubject.next(0);

expect(listener).to.have.been.calledOnce.and.to.have.been.calledWith(0);
}
);
}
useMockedPerformance(
(mockPerformance) => {
let valueStream;
let pulseStream;
let valueSubject;
let pulseSubject;
let listener;

beforeEach(
() => {
valueSubject = new IndefiniteSubject();
pulseSubject = new IndefiniteSubject();
valueStream = MotionObservable.from(valueSubject);
pulseStream = MotionObservable.from(pulseSubject);
listener = stub();
}
);

it(`should not dispatch unless pulse does`,
() => {
valueStream.velocity(pulseStream).subscribe(listener);

valueSubject.next(0);
valueSubject.next(1);
valueSubject.next(2);

expect(listener).not.to.have.been.called;
}
);

it(`should dispatch every time it receives a value is pulse isn't supplied`,
() => {
valueStream.velocity().subscribe(listener);

valueSubject.next(0);
mockPerformance.increment(1);
valueSubject.next(1);

expect(listener).to.have.been.calledTwice.and.to.have.been.calledWith(0).and.to.have.been.calledWith(1);
}
);

it(`should dispatch 0 if pulse dispatches before it's received values from upstream`,
() => {
valueStream.velocity(pulseStream).subscribe(listener);

pulseSubject.next(0);

expect(listener).to.have.been.calledOnce.and.to.have.been.calledWith(0);
}
);
}
)
);
28 changes: 28 additions & 0 deletions packages/testing-utils/src/Performance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/** @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.
*/

export class Performance {
_currentTime: number = Math.round(Math.random() * 10000);

now = () => {
return this._currentTime;
}

increment(amount = 16) {
this._currentTime += amount;
}
}
export default Performance;
8 changes: 8 additions & 0 deletions packages/testing-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@
* under the License.
*/

export * from './Performance';
export { default as Performance } from './Performance';

export * from './createMockObserver';
export { default as createMockObserver } from './createMockObserver';

export * from './useMockedRAF';
export { default as useMockedRAF } from './useMockedRAF';

export * from './useMockedPerformance';
export { default as useMockedPerformance } from './useMockedPerformance';

export default undefined;
55 changes: 55 additions & 0 deletions packages/testing-utils/src/useMockedPerformance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/** @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.
*/

import {
after,
before,
} from 'mocha-sugar-free';

import {
SinonStub,
stub,
} from 'sinon';

import {
Performance,
} from './Performance';

export type PerformanceClosure = (performance: Performance) => any;

/**
* Replaces window.requestAnimationFrame with a mock for the duration of a mocha
* testing suite.
*/
export default function useMockedPerformance(closure: PerformanceClosure) {
return () => {
const mockPerformance = new Performance();

before(
() => {
stub(performance, 'now').callsFake(mockPerformance.now);
}
);

after(
() => {
(performance.now as SinonStub).restore();
}
);

return closure(mockPerformance);
};
};

0 comments on commit 82c1546

Please sign in to comment.