22
33const Promise = require ( 'bluebird' ) ;
44const _ = require ( 'lodash' ) ;
5- const promiseUtils = require ( 'q-promise-utils' ) ;
65
76const CaptureSession = require ( 'lib/capture-session' ) ;
87const ActionsBuilder = require ( 'lib/tests-api/actions-builder' ) ;
@@ -17,15 +16,13 @@ describe('capture session', () => {
1716
1817 beforeEach ( ( ) => {
1918 imageStub = sinon . createStubInstance ( Image ) ;
20- sandbox . stub ( promiseUtils ) ;
21- promiseUtils . sequence . returns ( Promise . resolve ( ) ) ;
2219
2320 sandbox . stub ( temp ) ;
2421 } ) ;
2522
2623 afterEach ( ( ) => sandbox . restore ( ) ) ;
2724
28- describe ( 'runActions ' , ( ) => {
25+ describe ( 'run methods ' , ( ) => {
2926 let browser ;
3027 let session ;
3128
@@ -34,67 +31,78 @@ describe('capture session', () => {
3431 session = new CaptureSession ( browser ) ;
3532 } ) ;
3633
37- it ( 'should perform passed action sequence' , ( ) => {
38- const actions = [ ] ;
34+ describe ( 'runActions' , ( ) => {
35+ it ( 'should call action in associated browser and with "postActions"' , ( ) => {
36+ const action = sinon . spy ( ) . named ( 'action' ) ;
3937
40- session . runActions ( actions ) ;
41-
42- assert . calledOnce ( promiseUtils . sequence ) ;
43- assert . calledWith ( promiseUtils . sequence , actions ) ;
44- } ) ;
45-
46- it ( 'should perform actions sequence in associated browser' , ( ) => {
47- session . runActions ( [ ] ) ;
38+ return session . runActions ( [ action ] )
39+ . then ( ( ) => assert . calledOnceWith ( action , browser , sinon . match . instanceOf ( ActionsBuilder ) ) ) ;
40+ } ) ;
4841
49- assert . calledWith ( promiseUtils . sequence , sinon . match . any , browser ) ;
50- } ) ;
42+ it ( 'should perform all actions with the same postActions instance' , ( ) => {
43+ const action = sinon . spy ( ) . named ( 'action' ) ;
44+ sandbox . stub ( ActionsBuilder . prototype , '__constructor' ) . returnsArg ( 0 ) ;
5145
52- it ( 'should peform actions sequence with postActions' , ( ) => {
53- session . runActions ( [ ] ) ;
46+ return session . runActions ( [ action ] )
47+ . then ( ( ) => session . runActions ( [ action ] ) )
48+ . then ( ( ) => assert . deepEqual ( action . firstCall . args [ 1 ] , action . secondCall . args [ 1 ] ) ) ;
49+ } ) ;
5450
55- assert . calledWith ( promiseUtils . sequence ,
56- sinon . match . any ,
57- sinon . match . any ,
58- sinon . match . instanceOf ( ActionsBuilder )
59- ) ;
60- } ) ;
51+ it ( 'should perform passed actions in order' , ( ) => {
52+ const mediator = sinon . spy ( ) . named ( 'mediator' ) ;
53+ const action1 = sinon . stub ( ) . named ( 'action1' ) . callsFake ( ( ) => Promise . delay ( 1 ) . then ( mediator ) ) ;
54+ const action2 = sinon . spy ( ) . named ( 'action2' ) ;
6155
62- it ( 'should perform all actions with the same postActions instance' , ( ) => {
63- sandbox . stub ( ActionsBuilder . prototype , '__constructor' ) . returnsArg ( 0 ) ;
56+ return session . runActions ( [ action1 , action2 ] )
57+ . then ( ( ) => assert . callOrder ( action1 , mediator , action2 ) ) ;
58+ } ) ;
6459
65- session . runActions ( [ ] ) ;
66- session . runActions ( [ ] ) ;
60+ it ( 'should reject if any of passed actions rejected' , ( ) => {
61+ const action1 = sinon . stub ( ) . named ( 'action1' ) . returns ( Promise . resolve ( ) ) ;
62+ const action2 = sinon . stub ( ) . named ( 'action2' ) . returns ( Promise . reject ( 'foo' ) ) ;
6763
68- assert . equal (
69- promiseUtils . sequence . firstCall . args [ 2 ] ,
70- promiseUtils . sequence . secondCall . args [ 2 ]
71- ) ;
64+ return assert . isRejected ( session . runActions ( [ action1 , action2 ] ) , / f o o / ) ;
65+ } ) ;
7266 } ) ;
73- } ) ;
7467
75- describe ( 'runPostActions' , ( ) => {
76- it ( 'should not pass postActions while performing postActions' , ( ) => {
77- const browser = { config : { } } ;
78- const session = new CaptureSession ( browser ) ;
68+ describe ( 'runPostActions' , ( ) => {
69+ it ( 'should call action in associated browser' , ( ) => {
70+ const action = sinon . spy ( ) . named ( 'action' ) ;
7971
80- session . runPostActions ( ) ;
72+ sandbox . stub ( ActionsBuilder . prototype , '__constructor' ) . callsFake ( ( postActions ) => {
73+ postActions . push ( action ) ;
74+ } ) ;
8175
82- assert . lengthOf ( promiseUtils . sequence . firstCall . args , 2 ) ;
83- } ) ;
76+ return session . runActions ( [ action ] )
77+ . then ( ( ) => session . runPostActions ( ) )
78+ . then ( ( ) => assert . deepEqual ( action . secondCall . args , [ browser ] ) ) ;
79+ } ) ;
8480
85- it ( 'should perform post actions in reverse order' , ( ) => {
86- const browser = { config : { } } ;
87- const session = new CaptureSession ( browser ) ;
81+ it ( 'should perform post actions in reverse order' , ( ) => {
82+ const mediator = sinon . spy ( ) . named ( 'mediator' ) ;
83+ const action1 = sinon . spy ( ) . named ( 'action1' ) ;
84+ const action2 = sinon . stub ( ) . named ( 'action2' ) . callsFake ( ( ) => Promise . delay ( 1 ) . then ( mediator ) ) ;
8885
89- sandbox . stub ( ActionsBuilder . prototype , '__constructor' ) . callsFake ( ( postActions ) => {
90- postActions . push ( 1 , 2 , 3 ) ;
86+ sandbox . stub ( ActionsBuilder . prototype , '__constructor' ) . callsFake ( ( postActions ) => {
87+ postActions . push ( action1 , action2 ) ;
88+ } ) ;
89+
90+ return session . runActions ( [ action1 , action2 ] )
91+ . then ( ( ) => session . runPostActions ( ) )
92+ . then ( ( ) => assert . callOrder ( action2 , mediator , action1 ) ) ;
9193 } ) ;
92- session . runActions ( [ ] ) ;
93- session . runActions ( [ ] ) ;
9494
95- session . runPostActions ( ) ;
95+ it ( 'should reject if any of post actions rejected' , ( ) => {
96+ const action1 = sinon . stub ( ) . named ( 'action1' ) . returns ( Promise . resolve ( ) ) ;
97+ const action2 = sinon . stub ( ) . named ( 'action2' ) . returns ( Promise . reject ( 'foo' ) ) ;
9698
97- assert . calledWith ( promiseUtils . sequence , [ 3 , 2 , 1 , 3 , 2 , 1 ] ) ;
99+ sandbox . stub ( ActionsBuilder . prototype , '__constructor' ) . callsFake ( ( postActions ) => {
100+ postActions . push ( action1 , action2 ) ;
101+ } ) ;
102+
103+ return session . runActions ( [ action1 , action2 ] )
104+ . catch ( ( ) => assert . isRejected ( session . runPostActions ( ) , / f o o / ) ) ;
105+ } ) ;
98106 } ) ;
99107 } ) ;
100108
0 commit comments