Skip to content

Commit c339c22

Browse files
authored
Merge pull request #2634 from Tencent/feat-onroute
feat: added routed lifecycle
2 parents 71ebedf + d054bfd commit c339c22

File tree

5 files changed

+345
-117
lines changed

5 files changed

+345
-117
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = function createSelectorQuery() {
2+
return {
3+
query(selector) {
4+
return {
5+
mock: true,
6+
selector
7+
};
8+
}
9+
};
10+
};

packages/core/test/mock/wxapi.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
class MockWxAPI {
5+
constructor() {
6+
this._wx = {};
7+
this.init();
8+
}
9+
10+
init() {
11+
const files = fs.readdirSync(path.join(__dirname, 'api'));
12+
files.forEach(file => {
13+
const api = file.replace('.js', '');
14+
this._wx[api] = require('./api/' + api);
15+
});
16+
return this;
17+
}
18+
19+
mock(api, fn) {
20+
if (arguments.length === 2) {
21+
this._wx[api] = fn;
22+
}
23+
global.wx = this._wx;
24+
return this;
25+
}
26+
27+
unmock(api) {
28+
if (api) {
29+
delete this._wx[api];
30+
global.wx = this._wx;
31+
} else {
32+
this._wx = {};
33+
delete global.wx;
34+
}
35+
return this;
36+
}
37+
}
38+
39+
module.exports = MockWxAPI;
Lines changed: 150 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
const expect = require('chai').expect;
2-
import { getLifeCycle } from '../../../weapp/init';
2+
import { getLifeCycle, patchAppLifecycle, patchLifecycle } from '../../../weapp/init';
33
import { 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+
512
describe('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
});

packages/core/test/weapp/observer/observerPath.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ describe('weapp observer observerPath', function() {
5555
initData(vm, { a: { b: { c: { d: 123 } } }, x: {}, y: {} });
5656

5757
// eslint-disable-next-line
58-
console.log('complex path: Object');
5958
const data = vm._data;
6059
data.x = data.a.b.c;
6160
data.y = data.x;

0 commit comments

Comments
 (0)