GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — :white_check_mark: improve lifecycle testing · nullstack/nullstack@ea7584a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • client
    • client.js
  • shared
    • generateTree.js
  • tests/src
    • Application.njs
    • FullStackLifecycle.njs
    • FullStackLifecycle.test.js
    • PersistentComponent.njs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎client/client.js‎

Copy file name to clipboardExpand all lines: client/client.js
+5-4Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import router from './router';
2-
import rerender from './rerender';
3-
import context, { generateContext } from './context';
4-
51
import generateTree from '../shared/generateTree';
62
import { loadPlugins } from '../shared/plugins';
3+
import context, { generateContext } from './context';
4+
import rerender from './rerender';
5+
import router from './router';
6+
77

88
const client = {};
99

@@ -53,6 +53,7 @@ client.processLifecycleQueues = async function () {
5353
for (const instance of initiationQueue) {
5454
instance.initiate && await instance.initiate();
5555
instance._self.initiated = true;
56+
instance.launch && instance.launch()
5657
}
5758
if (initiationQueue.length) {
5859
client.update();
Collapse file

‎shared/generateTree.js‎

Copy file name to clipboardExpand all lines: shared/generateTree.js
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ async function generateBranch(parent, node, depth, scope) {
5858
}
5959
}
6060
let shouldHydrate = false;
61-
const shouldLaunch = !instance._self.prerendered || (instance._self.persistent && instance._self.terminated)
61+
const shouldLaunch = instance._self.initiated && (
62+
!instance._self.prerendered ||
63+
(instance._self.persistent && instance._self.terminated)
64+
)
6265
if (instance._self.terminated) {
6366
shouldHydrate = true;
6467
instance._self.terminated = false;
Collapse file

‎tests/src/Application.njs‎

Copy file name to clipboardExpand all lines: tests/src/Application.njs
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class Application extends Nullstack {
6363
<a href="/static-this"> static this </a>
6464
<a href="/routes-and-params/a"> router with params </a>
6565
<a href="/undefined-nodes"> undefined nodes </a>
66+
<a href="/full-stack-lifecycle"> lifecycle </a>
6667
</div>
6768
<RenderableComponent route="/renderable-component" />
6869
<StatefulComponent route="/stateful-component" />
Collapse file

‎tests/src/FullStackLifecycle.njs‎

Copy file name to clipboardExpand all lines: tests/src/FullStackLifecycle.njs
+14-11Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,41 @@ class FullStackLifecycle extends Nullstack {
88
hydrated = false;
99
updated = false;
1010

11-
prepare() {
11+
prepare({ environment }) {
1212
this.prepared = true;
13+
this.prepareEnv = environment.client ? 'client' : 'server'
1314
}
1415

15-
async initiate() {
16-
this.initiated = true;
16+
async initiate({ environment }) {
17+
this.initiated = this.prepared;
18+
this.initiateEnv = environment.client ? 'client' : 'server'
1719
}
1820

19-
launch() {
20-
this.launched = true
21+
launch({ environment }) {
22+
this.launched = this.initiated;
23+
this.launchEnv = environment.client ? 'client' : 'server'
2124
}
2225

2326
hydrate() {
24-
this.hydrated = true;
27+
this.hydrated = this.launched;
2528
}
2629

2730
update() {
2831
if (!this.updated) {
29-
this.updated = true;
32+
this.updated = this.hydrated;
3033
}
3134
}
3235

3336
async terminate({ params }) {
34-
params.terminated = true;
37+
params.terminated = this.updated;
3538
}
3639

3740
render() {
3841
return (
3942
<div class="FullStackLifecycle">
40-
<div data-prepared={this.prepared} />
41-
<div data-initiated={this.initiated} />
42-
<div data-launched={this.launched} />
43+
<div data-prepared={this.prepared} data-prepare-env={this.prepareEnv} />
44+
<div data-initiated={this.initiated} data-initiate-env={this.initiateEnv} />
45+
<div data-launched={this.launched} data-launch-env={this.launchEnv} />
4346
<div data-hydrated={this.hydrated} />
4447
<div data-updated={this.updated} />
4548
<a href="/"> Terminate </a>
Collapse file

‎tests/src/FullStackLifecycle.test.js‎

Copy file name to clipboardExpand all lines: tests/src/FullStackLifecycle.test.js
+93-4Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,129 @@
11
const puppeteer = require('puppeteer');
22

33
let browser;
4-
let page;
54

65
beforeAll(async () => {
76
browser = await puppeteer.launch();
8-
page = await browser.newPage();
9-
await page.goto('http://localhost:6969/full-stack-lifecycle');
107
});
118

12-
describe('FullStackLifecycle', () => {
9+
describe('FullStackLifecycle ssr', () => {
10+
11+
let page;
12+
13+
beforeAll(async () => {
14+
page = await browser.newPage();
15+
await page.goto('http://localhost:6969/full-stack-lifecycle');
16+
});
1317

1418
test('prepare should run', async () => {
1519
await page.waitForSelector('[data-prepared]');
1620
const element = await page.$('[data-prepared]');
1721
expect(element).toBeTruthy();
1822
});
1923

24+
test('prepare should run in the server for ssr', async () => {
25+
await page.waitForSelector('[data-prepare-env="server"]');
26+
const element = await page.$('[data-prepare-env="server"]');
27+
expect(element).toBeTruthy();
28+
});
29+
2030
test('initiate should run', async () => {
2131
await page.waitForSelector('[data-initiated]');
2232
const element = await page.$('[data-initiated]');
2333
expect(element).toBeTruthy();
2434
});
2535

36+
test('initiate should run in the server for ssr', async () => {
37+
await page.waitForSelector('[data-initiate-env="server"]');
38+
const element = await page.$('[data-initiate-env="server"]');
39+
expect(element).toBeTruthy();
40+
});
41+
42+
test('launch should run', async () => {
43+
await page.waitForSelector('[data-launched]');
44+
const element = await page.$('[data-launched]');
45+
expect(element).toBeTruthy();
46+
});
47+
48+
test('launch should run in the server for ssr', async () => {
49+
await page.waitForSelector('[data-launch-env="server"]');
50+
const element = await page.$('[data-launch-env="server"]');
51+
expect(element).toBeTruthy();
52+
});
53+
2654
test('hydrate should run', async () => {
2755
await page.waitForSelector('[data-hydrated]');
2856
const element = await page.$('[data-hydrated]');
2957
expect(element).toBeTruthy();
3058
});
3159

60+
test('update should run', async () => {
61+
await page.waitForSelector('[data-updated]');
62+
const element = await page.$('[data-updated]');
63+
expect(element).toBeTruthy();
64+
});
65+
66+
test('terminate should run', async () => {
67+
await page.click('a[href="/"]');
68+
await page.waitForFunction(() => location.search == '?terminated=true');
69+
const element = await page.$('.FullStackLifecycle');
70+
expect(element).toBeFalsy();
71+
});
72+
73+
});
74+
75+
describe('FullStackLifecycle spa', () => {
76+
77+
let page;
78+
79+
beforeAll(async () => {
80+
page = await browser.newPage();
81+
await page.goto('http://localhost:6969/');
82+
await page.click('a[href="/full-stack-lifecycle"]')
83+
});
84+
85+
test('prepare should run', async () => {
86+
await page.waitForSelector('[data-prepared]');
87+
const element = await page.$('[data-prepared]');
88+
expect(element).toBeTruthy();
89+
});
90+
91+
test('prepare should run in the client for spa', async () => {
92+
await page.waitForSelector('[data-prepare-env="client"]');
93+
const element = await page.$('[data-prepare-env="client"]');
94+
expect(element).toBeTruthy();
95+
});
96+
97+
test('initiate should run', async () => {
98+
await page.waitForSelector('[data-initiated]');
99+
const element = await page.$('[data-initiated]');
100+
expect(element).toBeTruthy();
101+
});
102+
103+
test('initiate should run in the client for spa', async () => {
104+
await page.waitForSelector('[data-initiate-env="client"]');
105+
const element = await page.$('[data-initiate-env="client"]');
106+
expect(element).toBeTruthy();
107+
});
108+
32109
test('launch should run', async () => {
33110
await page.waitForSelector('[data-launched]');
34111
const element = await page.$('[data-launched]');
35112
expect(element).toBeTruthy();
36113
});
37114

115+
test('launch should run in the client for spa', async () => {
116+
await page.waitForSelector('[data-launch-env="client"]');
117+
const element = await page.$('[data-launch-env="client"]');
118+
expect(element).toBeTruthy();
119+
});
120+
121+
test('hydrate should run', async () => {
122+
await page.waitForSelector('[data-hydrated]');
123+
const element = await page.$('[data-hydrated]');
124+
expect(element).toBeTruthy();
125+
});
126+
38127
test('update should run', async () => {
39128
await page.waitForSelector('[data-updated]');
40129
const element = await page.$('[data-updated]');
Collapse file

‎tests/src/PersistentComponent.njs‎

Copy file name to clipboardExpand all lines: tests/src/PersistentComponent.njs
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ class PersistentComponent extends Nullstack {
1313
this.count = -1;
1414
}
1515

16-
launch() {
17-
this.launchCount++
16+
launch({ self }) {
17+
if (self.initiated) {
18+
this.launchCount++
19+
}
1820
}
1921

2022
async hydrate() {

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.