Skip to content

Commit 4af22a0

Browse files
test: added unit tests
1 parent 2c42a7e commit 4af22a0

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { Org } from '@salesforce/core';
2+
3+
import sinon = require('sinon');
4+
import { expect } from '@salesforce/command/lib/test';
5+
import { Connection, ExecuteAnonymousResult } from 'jsforce';
6+
import { AnonymousApexRunner } from '../../../../src/utils/apex/executor/AnonymousApexRunner';
7+
8+
describe('AnonymusApexRunner', () => {
9+
let org: Org;
10+
// let executeAnonymousStub: sinon.SinonStub;
11+
let sandboxStub: sinon.SinonSandbox;
12+
13+
beforeEach(async () => {
14+
sandboxStub = sinon.createSandbox();
15+
const getConnectionStub = sandboxStub.stub().returns({
16+
tooling: {
17+
executeAnonymous: () => {},
18+
},
19+
} as unknown as Connection);
20+
sandboxStub.stub(Org.prototype, 'getConnection').callsFake(getConnectionStub);
21+
org = new Org({});
22+
});
23+
24+
afterEach(() => {
25+
// executeAnonymousStub.restore();
26+
sandboxStub.restore();
27+
});
28+
29+
it('should run anonymous Apex correctly', async () => {
30+
const anonymousApex = "System.debug('Hello, World');";
31+
const expectedResponse = { success: true };
32+
33+
const executeAnonymousResultStub = sandboxStub
34+
.stub()
35+
.returns(Promise.resolve({ success: true } as unknown as ExecuteAnonymousResult));
36+
sandboxStub.stub(Org.prototype.getConnection().tooling, 'executeAnonymous').callsFake(executeAnonymousResultStub);
37+
// .stub(org.getConnection().tooling.executeAnonymous(), 'executeAnonymous')
38+
// .returns(Promise.resolve({ success: true } as unknown as ExecuteAnonymousResult));
39+
40+
const result = await AnonymousApexRunner.run(org, anonymousApex);
41+
42+
expect(result).to.deep.equal(expectedResponse);
43+
sinon.assert.calledOnce(executeAnonymousResultStub);
44+
sinon.assert.calledWith(executeAnonymousResultStub, anonymousApex);
45+
});
46+
47+
it('should handle executeAnonymous errors', async () => {
48+
const anonymousApex = "System.debug 'Hello, World');";
49+
const executeAnonymousError = new Error('Error executing anonymous Apex');
50+
const executeAnonymousResultStub = sandboxStub.stub().rejects(executeAnonymousError);
51+
// executeAnonymousStub.rejects(executeAnonymousError);
52+
sandboxStub.stub(Org.prototype.getConnection().tooling, 'executeAnonymous').callsFake(executeAnonymousResultStub);
53+
54+
try {
55+
await AnonymousApexRunner.run(org, anonymousApex);
56+
throw new Error('Test should have thrown an error');
57+
} catch (error) {
58+
expect(error).to.equal(executeAnonymousError);
59+
sinon.assert.calledOnce(executeAnonymousResultStub);
60+
sinon.assert.calledWith(executeAnonymousResultStub, anonymousApex);
61+
}
62+
});
63+
});

0 commit comments

Comments
 (0)