Skip to content

Commit 662866a

Browse files
committed
chore: stricter unit tests
1 parent e9a0ab8 commit 662866a

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

test/unit/org/open.test.ts

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('org:open', () => {
2929
const testPath = '/lightning/whatever';
3030
const singleUseToken = (Math.random() + 1).toString(36).substring(2); // random string to simulate a single-use token
3131
const expectedDefaultSingleUseUrl = `${testOrg.instanceUrl}/secur/frontdoor.jsp?otp=${singleUseToken}`;
32-
const expectedSingleUseUrl = `${expectedDefaultSingleUseUrl}&startURL=${encodeURIComponent(testPath)}`;
32+
const expectedSingleUseUrlWithRedirect = `${expectedDefaultSingleUseUrl}&startURL=${encodeURIComponent(testPath)}`;
3333

3434
let sfCommandUxStubs: ReturnType<typeof stubSfCommandUx>;
3535

@@ -85,7 +85,7 @@ describe('org:open', () => {
8585
testPath,
8686
]);
8787
assert(response);
88-
expect(response.url).to.equal(expectedSingleUseUrl);
88+
expect(response.url).to.equal(expectedSingleUseUrlWithRedirect);
8989
});
9090

9191
describe('--source-file', () => {
@@ -113,12 +113,13 @@ describe('org:open', () => {
113113
fs.rmSync(apexDir, { force: true, recursive: true });
114114
});
115115

116-
it('--source-file to flexipage', async () => {
117-
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves({ Id: '123' });
116+
it('should open a flexipage in lightning app builder', async () => {
118117
const mockMetadataUrl = `${expectedDefaultSingleUseUrl}&startURL=${encodeURIComponent(
119118
'visualEditor/appBuilder.app?pageId=123'
120119
)}`;
121-
$$.SANDBOX.stub(Org.prototype, 'getMetadataUIURL').resolves(mockMetadataUrl);
120+
$$.SANDBOX.stub(Org.prototype, 'getMetadataUIURL')
121+
.withArgs('FlexiPage', flexipagePath)
122+
.resolves(mockMetadataUrl);
122123

123124
const response = await OrgOpenCommand.run([
124125
'--json',
@@ -129,12 +130,14 @@ describe('org:open', () => {
129130
flexipagePath,
130131
]);
131132

132-
expect(decodeURIComponent(response.url)).to.include('visualEditor/appBuilder.app?pageId=123');
133+
expect(response.url).to.equal(
134+
`${expectedDefaultSingleUseUrl}&startURL=visualEditor%2FappBuilder.app%3FpageId%3D123`
135+
);
133136
});
134137

135-
it('--source-file to an ApexPage', async () => {
138+
it('should open an apex page', async () => {
136139
const mockMetadataUrl = `${expectedDefaultSingleUseUrl}&startURL=${encodeURIComponent('/apex/test')}`;
137-
$$.SANDBOX.stub(Org.prototype, 'getMetadataUIURL').resolves(mockMetadataUrl);
140+
$$.SANDBOX.stub(Org.prototype, 'getMetadataUIURL').withArgs('ApexPage', apexPath).resolves(mockMetadataUrl);
138141

139142
const response = await OrgOpenCommand.run([
140143
'--json',
@@ -144,10 +147,11 @@ describe('org:open', () => {
144147
'--source-file',
145148
apexPath,
146149
]);
147-
expect(decodeURIComponent(response.url)).to.include('&startURL=/apex/test');
150+
151+
expect(response.url).to.equal(`${expectedDefaultSingleUseUrl}&startURL=%2Fapex%2Ftest`);
148152
});
149153

150-
it('--source-file when flexipage query errors', async () => {
154+
it('should return the frontdoor URL with no redirects if unable to resolve source file', async () => {
151155
$$.SANDBOX.stub(Org.prototype, 'getMetadataUIURL').rejects(new Error('Metadata query failed'));
152156
const response = await OrgOpenCommand.run([
153157
'--json',
@@ -160,19 +164,6 @@ describe('org:open', () => {
160164

161165
expect(response.url).to.equal(expectedDefaultSingleUseUrl);
162166
});
163-
164-
it('--source-file to neither flexipage or apexpage', async () => {
165-
const response = await OrgOpenCommand.run([
166-
'--json',
167-
'--targetusername',
168-
testOrg.username,
169-
'--urlonly',
170-
'--source-file',
171-
apexDir,
172-
]);
173-
174-
expect(response.url).to.equal(expectedDefaultSingleUseUrl);
175-
});
176167
});
177168

178169
it('can read url from env', async () => {
@@ -181,7 +172,7 @@ describe('org:open', () => {
181172
const response = await OrgOpenCommand.run(['--json', '--targetusername', testOrg.username, '--urlonly']);
182173
assert(response);
183174
testJsonStructure(response);
184-
expect(response.url).to.equal(expectedSingleUseUrl);
175+
expect(response.url).to.equal(expectedSingleUseUrlWithRedirect);
185176
expect(spies.get('requestGet').callCount).to.equal(1);
186177
delete process.env.FORCE_OPEN_URL;
187178
});
@@ -212,7 +203,7 @@ describe('org:open', () => {
212203
const response = await OrgOpenCommand.run(['--json', '--targetusername', testOrg.username, '--path', testPath]);
213204
assert(response);
214205
testJsonStructure(response);
215-
expect(response.url).to.equal(expectedSingleUseUrl);
206+
expect(response.url).to.equal(expectedSingleUseUrlWithRedirect);
216207

217208
expect(spies.get('resolver').callCount).to.equal(1);
218209
});
@@ -243,7 +234,7 @@ describe('org:open', () => {
243234
const response = await OrgOpenCommand.run(['--json', '--targetusername', testOrg.username, '--path', testPath]);
244235
assert(response);
245236
testJsonStructure(response);
246-
expect(response.url).to.equal(expectedSingleUseUrl);
237+
expect(response.url).to.equal(expectedSingleUseUrlWithRedirect);
247238
expect(spies.get('resolver').callCount).to.equal(0);
248239
delete process.env.SFDX_CONTAINER_MODE;
249240
});
@@ -254,7 +245,7 @@ describe('org:open', () => {
254245
const response = await OrgOpenCommand.run(['--json', '--targetusername', testOrg.username, '--path', testPath]);
255246
assert(response);
256247
testJsonStructure(response);
257-
expect(response.url).to.equal(expectedSingleUseUrl);
248+
expect(response.url).to.equal(expectedSingleUseUrlWithRedirect);
258249
expect(spies.get('resolver').callCount).to.equal(1);
259250
delete process.env.SF_DOMAIN_RETRY;
260251
});
@@ -278,7 +269,7 @@ describe('org:open', () => {
278269
await OrgOpenCommand.run(['--targetusername', testOrg.username, '--path', testPath, '--urlonly']);
279270

280271
expect(sfCommandUxStubs.logSuccess.firstCall.args[0]).to.deep.equal(
281-
messages.getMessage('humanSuccess', [testOrg.orgId, testOrg.username, expectedSingleUseUrl])
272+
messages.getMessage('humanSuccess', [testOrg.orgId, testOrg.username, expectedSingleUseUrlWithRedirect])
282273
);
283274
});
284275

0 commit comments

Comments
 (0)