11const expect = require ( 'chai' ) . expect ;
2- import { getLifeCycle } from '../../../weapp/init' ;
2+ import { getLifeCycle , patchAppLifecycle , patchLifecycle } from '../../../weapp/init' ;
33import { WEAPP_APP_LIFECYCLE } from '../../../shared' ;
44
5+ import MockWxAPI from '../../mock/wxapi' ;
6+
7+ const mockapi = new MockWxAPI ( ) ;
8+
9+ const allPages = [ ] ;
10+ const pageIndex = - 1 ;
11+
512describe ( 'weapp life cycles' , ( ) => {
13+ before ( ( ) => {
14+ mockapi . mock ( ) ;
15+
16+ global . getCurrentPages = function ( ) {
17+ return allPages ;
18+ } ;
19+ } ) ;
20+ after ( ( ) => {
21+ mockapi . unmock ( ) ;
22+ } ) ;
23+
624 it ( 'should add wepy.app life cycles for string' , ( ) => {
725 const rel = {
826 lifecycle : {
927 app : 'onSomeNewFeature'
1028 }
1129 } ;
1230 const lifeCycles = getLifeCycle ( WEAPP_APP_LIFECYCLE , rel , 'app' ) ;
13- expect ( lifeCycles ) . to . deep . equal ( [
14- ...WEAPP_APP_LIFECYCLE ,
15- 'onSomeNewFeature'
16- ] ) ;
31+ expect ( lifeCycles ) . to . deep . equal ( [ ...WEAPP_APP_LIFECYCLE , 'onSomeNewFeature' ] ) ;
1732 } ) ;
1833
1934 it ( 'should add wepy.app life cycles for array' , ( ) => {
@@ -23,26 +38,146 @@ describe('weapp life cycles', () => {
2338 }
2439 } ;
2540 const lifeCycles = getLifeCycle ( WEAPP_APP_LIFECYCLE , rel , 'app' ) ;
26- expect ( lifeCycles ) . to . deep . equal ( [
27- ...WEAPP_APP_LIFECYCLE ,
28- 'onSomeNewFeature1' ,
29- 'onSomeNewFeature2'
30- ] ) ;
41+ expect ( lifeCycles ) . to . deep . equal ( [ ...WEAPP_APP_LIFECYCLE , 'onSomeNewFeature1' , 'onSomeNewFeature2' ] ) ;
3142 } ) ;
3243
3344 it ( 'should modify wepy.app life cycles for function' , ( ) => {
3445 const rel = {
3546 lifecycle : {
36- app : function ( lifecycles ) {
47+ app : function ( lifecycles ) {
3748 const newLifeCycles = lifecycles . filter ( l => l !== 'onError' ) ;
3849 return [ ...newLifeCycles , 'onSomeNewFeature' ] ;
3950 }
4051 }
4152 } ;
4253 const lifeCycles = getLifeCycle ( WEAPP_APP_LIFECYCLE , rel , 'app' ) ;
43- expect ( lifeCycles ) . to . deep . equal ( [
44- ...WEAPP_APP_LIFECYCLE . filter ( l => l !== 'onError' ) ,
45- 'onSomeNewFeature'
46- ] ) ;
54+ expect ( lifeCycles ) . to . deep . equal ( [ ...WEAPP_APP_LIFECYCLE . filter ( l => l !== 'onError' ) , 'onSomeNewFeature' ] ) ;
55+ } ) ;
56+
57+ it ( 'patchAppLifecycle' , ( ) => {
58+ const runState = {
59+ onLaunch1 : false ,
60+ onLaunch2 : false ,
61+ onShow : false
62+ } ;
63+ const appInstance = { } ;
64+ const appConfig = { } ;
65+ const options = {
66+ onShow ( ) {
67+ runState . onShow = true ;
68+ } ,
69+ onLaunch : [
70+ ( ) => {
71+ runState . onLaunch1 = true ;
72+ } ,
73+ ( ) => {
74+ runState . onLaunch2 = true ;
75+ }
76+ ]
77+ } ;
78+ const rel = { } ;
79+
80+ patchAppLifecycle ( appConfig , options , rel ) ;
81+
82+ expect ( appConfig . onLaunch ) . is . a ( 'function' ) ;
83+ expect ( appConfig . onShow ) . is . a ( 'function' ) ;
84+
85+ expect ( appConfig . onError ) . is . a ( 'undefined' ) ;
86+
87+ appConfig . onLaunch . call ( appInstance ) ;
88+
89+ appConfig . onShow . call ( appInstance ) ;
90+
91+ for ( const k in runState ) {
92+ expect ( runState [ k ] ) . to . be . equal ( true ) ;
93+ }
94+ } ) ;
95+
96+ it ( 'patchLifecycle componennt' , ( ) => {
97+ const runState = {
98+ ready : false
99+ } ;
100+ const compInstance = { } ;
101+ const output = { methods : { } } ;
102+ const options = {
103+ ready ( ) {
104+ runState . ready = true ;
105+ }
106+ } ;
107+ const rel = { } ;
108+
109+ patchLifecycle ( output , options , rel , true ) ;
110+
111+ expect ( output . created ) . is . a ( 'function' ) ;
112+
113+ output . created . call ( compInstance ) ;
114+
115+ output . ready . call ( compInstance ) ;
116+
117+ expect ( runState . ready ) . to . equal ( true ) ;
118+ } ) ;
119+
120+ it ( 'patchLifecycle page' , ( ) => {
121+ const runState = {
122+ routed : false ,
123+ testPageRouted : false
124+ } ;
125+ const indexPage = {
126+ is : 'somepage' ,
127+ route : 'pages/index' ,
128+ __wxExparserNodeId__ : '29d1dad1' ,
129+ __wxWebviewId__ : 1
130+ } ;
131+ const output = { methods : { } } ;
132+ const options = {
133+ routed ( oldRoute , newRoute ) {
134+ expect ( oldRoute ) . to . equal ( null ) ;
135+ expect ( newRoute . path ) . to . equal ( indexPage . route ) ;
136+ runState . routed = true ;
137+ }
138+ } ;
139+ const rel = { } ;
140+
141+ patchLifecycle ( output , options , rel , false ) ;
142+
143+ expect ( output . created ) . is . a ( 'function' ) ;
144+ expect ( output . attached ) . is . a ( 'function' ) ;
145+
146+ output . created . call ( indexPage ) ;
147+ output . attached . call ( indexPage ) ;
148+
149+ allPages . push ( indexPage ) ;
150+
151+ output . methods . onShow . call ( indexPage ) ;
152+
153+ expect ( runState . routed ) . to . equal ( true ) ;
154+
155+ const testPage = {
156+ is : 'testpage' ,
157+ route : 'pages/test' ,
158+ __wxExparserNodeId__ : '29d2dad2' ,
159+ __wxWebviewId__ : 2
160+ } ;
161+ const testOutput = { methods : { } } ;
162+ const testOutputOptions = {
163+ routed ( oldRoute , newRoute ) {
164+ expect ( oldRoute . path ) . to . equal ( indexPage . route ) ;
165+ expect ( newRoute . path ) . to . equal ( testPage . route ) ;
166+ runState . testPageRouted = true ;
167+ }
168+ } ;
169+
170+ patchLifecycle ( testOutput , testOutputOptions , rel , false ) ;
171+
172+ testOutput . created . call ( testPage ) ;
173+ testOutput . attached . call ( testPage ) ;
174+
175+ allPages . push ( testPage ) ;
176+
177+ testOutput . methods . onShow . call ( testPage ) ;
178+
179+ for ( const k in runState ) {
180+ expect ( runState [ k ] ) . to . be . equal ( true ) ;
181+ }
47182 } ) ;
48183} ) ;
0 commit comments