Skip to content

Commit c34418d

Browse files
committed
[chore/feat] Add testing to extensions, add Sanity Lifecycle
1 parent cfb00de commit c34418d

File tree

8 files changed

+179
-104
lines changed

8 files changed

+179
-104
lines changed

lib/Core.ts

+18
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ export const Core = {
305305
app.log.silly(Templates.silly.initialized)
306306
app.log.info(Templates.info.initialized)
307307
})
308+
app.once('spool:all:sane', () => {
309+
app.log.silly(Templates.silly.sane)
310+
app.log.info(Templates.info.sane)
311+
})
308312
app.once('fabrix:ready', () => {
309313
app.log.info(Templates.info.ready(app))
310314
app.log.debug(Templates.debug.ready(app))
@@ -331,6 +335,7 @@ export const Core = {
331335
const validatedEvents = spools.map(spool => `spool:${spool.name}:validated`)
332336
const configuredEvents = spools.map(spool => `spool:${spool.name}:configured`)
333337
const initializedEvents = spools.map(spool => `spool:${spool.name}:initialized`)
338+
const sanityEvents = spools.map(spool => `spool:${spool.name}:sane`)
334339

335340
app.after(configuredEvents).then(async () => {
336341
await this.createDefaultPaths(app)
@@ -343,6 +348,11 @@ export const Core = {
343348
app.after(initializedEvents)
344349
.then(() => {
345350
app.emit('spool:all:initialized')
351+
})
352+
353+
app.after(sanityEvents)
354+
.then(() => {
355+
app.emit('spool:all:sane')
346356
app.emit('fabrix:ready')
347357
})
348358
},
@@ -357,6 +367,14 @@ export const Core = {
357367
) {
358368
const lifecycle = spool.lifecycle
359369

370+
app.after((lifecycle.sanity.listen).concat('spool:all:initialized'))
371+
.then(() => app.log.debug('spool: sanity check', spool.name))
372+
.then(() => spool.stage = 'sanity')
373+
.then(() => spool.sanity())
374+
.then(() => app.emit(`spool:${spool.name}:sane`))
375+
.then(() => spool.stage = 'sane')
376+
.catch(this.handlePromiseRejection)
377+
360378
app.after((lifecycle.initialize.listen).concat('spool:all:configured'))
361379
.then(() => app.log.debug('spool: initializing', spool.name))
362380
.then(() => spool.stage = 'initializing')

lib/Templates.ts

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const Templates = {
1010
start: 'Spooling up...',
1111
stop: 'Spooling down...',
1212
initialized: 'All spools are loaded.',
13+
sane: `Sanity check complete`,
1314
ready (app: FabrixApp) {
1415
const baseUrl = app.config.get('web.baseUrl') ||
1516
`http://${app.config.get('web.host') || 'localhost'}:${app.config.get('web.port') || '80'}`
@@ -54,6 +55,8 @@ export const Templates = {
5455
)
5556
},
5657

58+
sane: `Sanity checks keep us happy`,
59+
5760
initialized: `
5861
5962
,---. ,--. ,--.

lib/common/Spool.ts

+13
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export class Spool {
5656
initialize: {
5757
listen: [ ],
5858
emit: [ ]
59+
},
60+
sanity: {
61+
listen: [ ],
62+
emit: [ ]
5963
}
6064
}
6165
}
@@ -183,6 +187,15 @@ export class Spool {
183187

184188
}
185189

190+
/**
191+
* Check any configured or initialized state to prove that the Fabrix app in
192+
* fact does have the values specified by the lifecylce. Spools that require
193+
* runtime specifications should override this method
194+
*/
195+
sanity (): any {
196+
197+
}
198+
186199
/**
187200
* Unload this Spool. This method will instruct the spool to perform
188201
* any necessary cleanup with the expectation that the app will stop or reload

lib/common/interfaces/ILifecycle.ts

+4
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@ export interface ILifecycle {
66
initialize: {
77
listen: string[],
88
emit: string[]
9+
},
10+
sanity: {
11+
listen: string[],
12+
emit: string[]
913
}
1014
}

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fabrix/fabrix",
3-
"version": "1.0.6",
3+
"version": "1.0.7",
44
"description": "Strongly Typed Modern Web Application Framework for Node.js",
55
"keywords": [
66
"framework",
@@ -16,6 +16,8 @@
1616
"sails.js",
1717
"trails.js",
1818
"fabrix",
19+
"fabrix.js",
20+
"fabrix.ts",
1921
"typescript",
2022
"server",
2123
"graphql"

test/lib/Core.test.js

+35
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,39 @@ describe('lib.Core', () => {
7373
assert.throws(() => lib.Core.handlePromiseRejection(new Error('Promise Rejection Test')), Error)
7474
})
7575
})
76+
describe('#mergeExtensions', () => {
77+
const B = class B {
78+
constructor() {
79+
this.foo = 'bar'
80+
this.extensions = {
81+
foo: {
82+
get: () => {
83+
return this.foo
84+
},
85+
set: (newValue) => {
86+
this.foo = newValue
87+
},
88+
enumerable: true,
89+
configurable: true
90+
}
91+
}
92+
return this
93+
}
94+
}
95+
96+
const A = class A {
97+
constructor(b) {
98+
lib.Core.mergeExtensions(this, b)
99+
}
100+
}
101+
102+
it('should merge extensions from a spool and place them in the app', () => {
103+
const b = new B()
104+
const a = new A(b)
105+
106+
assert.equal(a.foo, 'bar')
107+
a.foo = 'baz'
108+
assert.equal(a.foo, 'baz')
109+
})
110+
})
76111
})

0 commit comments

Comments
 (0)