-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
observer.integration.js
89 lines (78 loc) · 2.9 KB
/
observer.integration.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/cli
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
const path = require('path');
const assert = require('yeoman-assert');
const testlab = require('@loopback/testlab');
const TestSandbox = testlab.TestSandbox;
const generator = path.join(__dirname, '../../../generators/observer');
const SANDBOX_FILES = require('../../fixtures/observer').SANDBOX_FILES;
const testUtils = require('../../test-utils');
// Test Sandbox
const SANDBOX_PATH = path.resolve(__dirname, '..', '.sandbox');
const sandbox = new TestSandbox(SANDBOX_PATH);
describe('lb4 observer', () => {
beforeEach('reset sandbox', async () => {
await sandbox.reset();
});
describe('valid generation of observers', () => {
it('generates a basic observer from command line arguments', async () => {
await testUtils
.executeGenerator(generator)
.inDir(SANDBOX_PATH, () =>
testUtils.givenLBProject(SANDBOX_PATH, {
additionalFiles: SANDBOX_FILES,
}),
)
.withArguments('myObserver');
verifyGeneratedScript();
});
it('generates a basic observer from CLI with group', async () => {
await testUtils
.executeGenerator(generator)
.inDir(SANDBOX_PATH, () =>
testUtils.givenLBProject(SANDBOX_PATH, {
additionalFiles: SANDBOX_FILES,
}),
)
.withArguments('myObserver --group myGroup');
verifyGeneratedScript('myGroup');
});
it('generates a observer from a config file', async () => {
await testUtils
.executeGenerator(generator)
.inDir(SANDBOX_PATH, () =>
testUtils.givenLBProject(SANDBOX_PATH, {
additionalFiles: SANDBOX_FILES,
}),
)
.withArguments('--config myobserverconfig.json');
verifyGeneratedScript();
});
});
});
// Sandbox constants
const SCRIPT_APP_PATH = 'src/observers';
const INDEX_FILE = path.join(SANDBOX_PATH, SCRIPT_APP_PATH, 'index.ts');
function verifyGeneratedScript(group = '') {
const expectedFile = path.join(
SANDBOX_PATH,
SCRIPT_APP_PATH,
'my-observer.observer.ts',
);
assert.file(expectedFile);
assert.fileContent(expectedFile, 'lifeCycleObserver, // The decorator');
assert.fileContent(
expectedFile,
/export class MyObserverObserver implements LifeCycleObserver {/,
);
assert.fileContent(expectedFile, `@lifeCycleObserver('${group}')`);
assert.fileContent(expectedFile, /async start\(\): Promise\<void\> {/);
assert.fileContent(expectedFile, /\/\/ Add your logic for start/);
assert.fileContent(expectedFile, /\/\/ Add your logic for stop/);
assert.fileContent(expectedFile, /async stop\(\): Promise\<void\> {/);
assert.file(INDEX_FILE);
assert.fileContent(INDEX_FILE, /export \* from '.\/my-observer.observer';/);
}