55 * Public License, v. 2.0 that can be found in the LICENSE file.
66 */
77
8- import { describe , expect , it , beforeEach , vi , afterEach , afterAll } from 'vitest' ;
8+ import { describe , expect , it , beforeEach , vi , afterEach , afterAll , MockInstance } from 'vitest' ;
99import { Composition } from './composition' ;
1010import { Clip , TextClip } from '../clips' ;
1111import { AudioTrack , CaptionTrack , HtmlTrack , ImageTrack , TextTrack , Track , VideoTrack } from '../tracks' ;
1212import { Timestamp } from '../models' ;
1313
1414describe ( 'The composition' , ( ) => {
1515 let composition : Composition ;
16+ let computeMock : MockInstance < ( ) => void > ;
17+
1618 const frameMock = vi . fn ( ) ;
1719 const playMock = vi . fn ( ) ;
1820 const pauseMock = vi . fn ( ) ;
@@ -33,6 +35,7 @@ describe('The composition', () => {
3335 composition . state = 'IDLE' ;
3436
3537 localStorage . clear ( ) ;
38+ computeMock = vi . spyOn ( composition , 'computeFrame' ) ;
3639 } ) ;
3740
3841 it ( 'should initialize with default settings' , ( ) => {
@@ -173,7 +176,7 @@ describe('The composition', () => {
173176 expect ( composition . duration . seconds ) . toBe ( 0.5 ) ;
174177
175178 const seekMock = vi . spyOn ( track , 'seek' ) ;
176- const computeMock = vi . spyOn ( composition , 'computeFrame' ) ;
179+ computeMock . mockClear ( ) ;
177180
178181 const frameCallbacks : number [ ] = [ ] ;
179182 composition . on ( 'currentframe' , ( evt ) => frameCallbacks . push ( evt . detail ) ) ;
@@ -381,6 +384,36 @@ describe('The composition', () => {
381384 expect ( track1 . clips . length ) . toBe ( 2 ) ;
382385 } ) ;
383386
387+ it ( 'should redraw the composition when it changes' , async ( ) => {
388+ expect ( composition . duration . frames ) . toBe ( 0 ) ;
389+ expect ( computeMock ) . toBeCalledTimes ( 0 ) ;
390+
391+ const track = composition . createTrack ( 'base' ) ;
392+
393+ expect ( composition . duration . frames ) . toBe ( 0 ) ;
394+ expect ( computeMock ) . toBeCalledTimes ( 1 ) ;
395+
396+ await track . add ( new Clip ( { stop : 20 } ) ) ;
397+
398+ expect ( composition . duration . frames ) . toBe ( 20 ) ;
399+ expect ( computeMock ) . toBeCalledTimes ( 2 ) ;
400+
401+ const clip = await track . add ( new Clip ( { start : 30 , stop : 60 } ) ) ;
402+
403+ expect ( composition . duration . frames ) . toBe ( 60 ) ;
404+ expect ( computeMock ) . toBeCalledTimes ( 3 ) ;
405+
406+ clip . stop = 80 ;
407+
408+ expect ( composition . duration . frames ) . toBe ( 80 ) ;
409+ expect ( computeMock ) . toBeCalledTimes ( 4 ) ;
410+
411+ track . remove ( clip ) ;
412+
413+ expect ( composition . duration . frames ) . toBe ( 20 ) ;
414+ expect ( computeMock ) . toBeCalledTimes ( 5 ) ;
415+ } ) ;
416+
384417 afterEach ( ( ) => {
385418 frameMock . mockClear ( ) ;
386419 playMock . mockClear ( ) ;
0 commit comments