import { expect } from "chai" ; import * as sinon from "sinon" ; import { stubFrameTickr } from "./fakes.test" ; describe ( "frameRunnr" , ( ) => { describe ( "getPaused" , ( ) => { it ( "returns true before play has started" , ( ) => { const { frameTicker } = stubFrameTickr ( ) ; const paused = frameTicker . getPaused ( ) ; expect ( paused ) . to . be . equal ( true ) ; } ) ; it ( "returns false after play has started" , ( ) => { const { frameTicker } = stubFrameTickr ( ) ; frameTicker . play ( ) ; const paused = frameTicker . getPaused ( ) ; expect ( paused ) . to . be . equal ( false ) ; } ) ; it ( "returns true after play is paused" , ( ) => { const { frameTicker } = stubFrameTickr ( ) ; frameTicker . play ( ) ; frameTicker . pause ( ) ; const paused = frameTicker . getPaused ( ) ; expect ( paused ) . to . be . equal ( true ) ; } ) ; } ) ; describe ( "pause" , ( ) => { it ( "stops frame from executing when previously playing" , ( ) => { const frame = sinon . stub ( ) ; const interval = 10 ; const { clock , frameTicker } = stubFrameTickr ( { frame , interval } ) ; frameTicker . play ( ) ; frameTicker . pause ( ) ; clock . tick ( interval ) ; expect ( frame ) . to . have . callCount ( 1 ) ; } ) ; it ( "doesn't fire a pause event before being called" , ( ) => { const events = { pause : sinon . stub ( ) , } ; const { frameTicker } = stubFrameTickr ( { events } ) ; frameTicker . play ( ) ; expect ( events . pause ) . to . callCount ( 0 ) ; } ) ; it ( "fires a pause event when there is one" , ( ) => { const events = { pause : sinon . stub ( ) , } ; const { frameTicker } = stubFrameTickr ( { events } ) ; frameTicker . play ( ) ; frameTicker . pause ( ) ; expect ( events . pause ) . to . callCount ( 1 ) ; } ) ; } ) ; describe ( "play" , ( ) => { it ( "executes frame immediately when previously paused" , ( ) => { const frame = sinon . stub ( ) ; const { frameTicker } = stubFrameTickr ( { frame } ) ; frameTicker . play ( ) ; expect ( frame ) . to . have . callCount ( 1 ) ; } ) ; it ( "doesn't re-run frame when run twice" , ( ) => { const frame = sinon . stub ( ) ; const { frameTicker } = stubFrameTickr ( { frame } ) ; frameTicker . play ( ) ; frameTicker . play ( ) ; expect ( frame ) . to . have . callCount ( 1 ) ; } ) ; it ( "doesn't run frame again before the interval has passed" , ( ) => { const frame = sinon . stub ( ) ; const interval = 10 ; const { clock , frameTicker } = stubFrameTickr ( { frame , interval } ) ; frameTicker . play ( ) ; clock . tick ( interval - 1 ) ; expect ( frame ) . to . have . callCount ( 1 ) ; } ) ; it ( "runs frame again when the interval has passed" , ( ) => { const frame = sinon . stub ( ) ; const interval = 10 ; const { clock , frameTicker } = stubFrameTickr ( { frame , interval } ) ; frameTicker . play ( ) ; clock . tick ( interval ) ; expect ( frame ) . to . have . callCount ( 2 ) ; } ) ; it ( "doesn't fire a pause event before being called" , ( ) => { const events = { play : sinon . stub ( ) , } ; stubFrameTickr ( { events } ) ; expect ( events . play ) . to . callCount ( 0 ) ; } ) ; it ( "fires a pause event when there is one" , ( ) => { const events = { play : sinon . stub ( ) , } ; const { frameTicker } = stubFrameTickr ( { events } ) ; frameTicker . play ( ) ; expect ( events . play ) . to . callCount ( 1 ) ; } ) ; } ) ; describe ( "setInterval" , ( ) => { it ( "affects previously scheduled ticks" , ( ) => { const frame = sinon . stub ( ) ; const interval = 10 ; const { clock , frameTicker } = stubFrameTickr ( { frame , interval } ) ; frameTicker . play ( ) ; frameTicker . setInterval ( 100 ) ; clock . tick ( interval ) ; expect ( frame ) . to . have . callCount ( 1 ) ; } ) ; it ( "changes the schedule for future ticks" , ( ) => { const frame = sinon . stub ( ) ; const interval = 10 ; const newInterval = 100 ; const { clock , frameTicker } = stubFrameTickr ( { frame , interval } ) ; frameTicker . setInterval ( newInterval ) ; frameTicker . play ( ) ; clock . tick ( newInterval ) ; expect ( frame ) . to . have . callCount ( 2 ) ; } ) ; } ) ; describe ( "getInterval" , ( ) => { it ( "gets the initial interval when interval hasn't been changed" , ( ) => { const initialInterval = 10 ; const { frameTicker } = stubFrameTickr ( { interval : initialInterval , } ) ; const retrievedInterval = frameTicker . getInterval ( ) ; expect ( retrievedInterval ) . to . be . equal ( initialInterval ) ; } ) ; it ( "gets an updated interval when the interval has been changed" , ( ) => { const updatedInterval = 10 ; const { frameTicker } = stubFrameTickr ( { interval : updatedInterval / 2 , } ) ; frameTicker . setInterval ( updatedInterval ) ; const retrievedInterval = frameTicker . getInterval ( ) ; expect ( retrievedInterval ) . to . be . equal ( updatedInterval ) ; } ) ; } ) ; } ) ;