forked from salesforcecli/plugin-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembedding.nut.ts
More file actions
152 lines (133 loc) · 6.24 KB
/
Copy pathembedding.nut.ts
File metadata and controls
152 lines (133 loc) · 6.24 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright (c) 2026, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import path from 'node:path';
import { expect, config } from 'chai';
import { TestSession, execCmd } from '@salesforce/cli-plugins-testkit';
import assert from 'yeoman-assert';
config.truncateThreshold = 0;
describe('template generate lightning embedding:', () => {
let session: TestSession;
before(async () => {
session = await TestSession.create({
project: {},
devhubAuthStrategy: 'NONE',
});
});
after(async () => {
await session?.clean();
});
const lwcDir = (): string => path.join(session.project.dir, 'lwc');
const bundleFiles = (componentName: string): string[] => {
const camel = componentName.charAt(0).toLowerCase() + componentName.slice(1);
return ['.html', '.js', '.css', '.js-meta.xml'].map((suffix) => path.join(lwcDir(), camel, camel + suffix));
};
describe('Check lightning embedding creation', () => {
const name = 'MyEmbedding';
const src = 'https://app.example.com';
const shellTitle = 'Demo Embedding';
it('should scaffold an embedding LWC bundle with all four files', () => {
execCmd(
`template generate lightning embedding --name ${name} --src ${src} --sandbox allow-forms --shell-title "${shellTitle}" --output-dir ${lwcDir()}`,
{ ensureExitCode: 0 }
);
assert.file(bundleFiles(name));
});
it('should emit a <lightning-embedding> element in the generated html', () => {
execCmd(
`template generate lightning embedding --name ${name} --src ${src} --sandbox allow-forms --shell-title "${shellTitle}" --output-dir ${lwcDir()}`,
{ ensureExitCode: 0 }
);
const camel = name.charAt(0).toLowerCase() + name.slice(1);
assert.fileContent(path.join(lwcDir(), camel, `${camel}.html`), '<lightning-embedding');
});
it('should join multiple --sandbox tokens into a single space-separated attribute', () => {
execCmd(
`template generate lightning embedding --name MultiSandbox --src ${src} --sandbox allow-forms --sandbox allow-scripts --shell-title "${shellTitle}" --output-dir ${lwcDir()}`,
{ ensureExitCode: 0 }
);
assert.fileContent(
path.join(lwcDir(), 'multiSandbox', 'multiSandbox.html'),
'sandbox="allow-forms allow-scripts"'
);
});
it('should bind the src URL into the generated js as a reactive property', () => {
execCmd(
`template generate lightning embedding --name SrcBinding --src ${src} --sandbox allow-forms --shell-title "${shellTitle}" --output-dir ${lwcDir()}`,
{ ensureExitCode: 0 }
);
assert.fileContent(path.join(lwcDir(), 'srcBinding', 'srcBinding.js'), src);
});
it('should accept http URLs on localhost for local development', () => {
execCmd(
`template generate lightning embedding --name LocalDev --src http://localhost:3000 --sandbox allow-forms --shell-title "${shellTitle}" --output-dir ${lwcDir()}`,
{ ensureExitCode: 0 }
);
assert.fileContent(path.join(lwcDir(), 'localDev', 'localDev.js'), 'http://localhost:3000');
});
});
describe('lightning embedding failures', () => {
const baseFlags = '--name Foo --sandbox allow-forms --shell-title "Demo"';
it('should throw missing --name error', () => {
const stderr = execCmd(
'template generate lightning embedding --src https://app.example.com --sandbox allow-forms --shell-title "Demo"'
).shellOutput.stderr;
expect(stderr).to.contain('Missing required flag');
});
it('should throw missing --src error', () => {
const stderr = execCmd(
'template generate lightning embedding --name Foo --sandbox allow-forms --shell-title "Demo"'
).shellOutput.stderr;
expect(stderr).to.contain('Missing required flag');
});
it('should throw missing --sandbox error', () => {
const stderr = execCmd(
'template generate lightning embedding --name Foo --src https://app.example.com --shell-title "Demo"'
).shellOutput.stderr;
expect(stderr).to.contain('Missing required flag');
});
it('should throw missing --shell-title error (no fallback to --name)', () => {
const stderr = execCmd(
'template generate lightning embedding --name Foo --src https://app.example.com --sandbox allow-forms'
).shellOutput.stderr;
expect(stderr).to.contain('Missing required flag');
});
it('should reject http src on a non-localhost host', () => {
const stderr = execCmd(
`template generate lightning embedding --name Foo --src http://attacker.com --sandbox allow-forms --shell-title "Demo" --output-dir ${lwcDir()}`
).shellOutput.stderr;
expect(stderr).to.contain('HTTPS URL');
});
it('should reject non-http(s) protocols', () => {
const stderr = execCmd(
`template generate lightning embedding --name Foo --src ftp://example.com --sandbox allow-forms --shell-title "Demo" --output-dir ${lwcDir()}`
).shellOutput.stderr;
expect(stderr).to.contain('HTTPS URL');
});
it('should reject malformed --src input', () => {
const stderr = execCmd(
`template generate lightning embedding --name Foo --src not-a-url --sandbox allow-forms --shell-title "Demo" --output-dir ${lwcDir()}`
).shellOutput.stderr;
expect(stderr).to.contain('HTTPS URL');
});
it('should reject an unknown sandbox token', () => {
const stderr = execCmd(
`template generate lightning embedding ${baseFlags} --src https://app.example.com --sandbox allow-everything --output-dir ${lwcDir()}`,
{ ensureExitCode: 'nonZero' }
).shellOutput.stderr;
expect(stderr).to.contain('Expected --sandbox');
});
it('should throw missing lwc parent folder error when output-dir is not under lwc/', () => {
const stderr = execCmd(
`template generate lightning embedding --name Foo --src https://app.example.com --sandbox allow-forms --shell-title "Demo" --output-dir ${path.join(
session.project.dir,
'somewhere-else'
)}`
).shellOutput.stderr;
expect(stderr).to.match(/lwc/i);
});
});
});