Skip to content

Commit 125c689

Browse files
authored
fix: automock apex methods with valid wire adapters (#208)
1 parent 631cdef commit 125c689

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
"overrides": [
1616
{
17-
"files": ["src/lightning-stubs/**"],
17+
"files": ["src/lightning-stubs/**", "src/apex-stubs/**"],
1818
"parser": "babel-eslint",
1919
"parserOptions": {
2020
"sourceType": "module"

src/apex-stubs/method/method.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Copyright (c) 2021, salesforce.com, inc.
3+
* All rights reserved.
4+
* SPDX-License-Identifier: MIT
5+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
6+
*/
7+
import { createApexTestWireAdapter } from '@salesforce/wire-service-jest-util';
8+
9+
export default createApexTestWireAdapter(jest.fn());

src/resolver.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,36 @@ function resolveAsFile(name, extensions) {
4747
return undefined;
4848
}
4949

50-
function getLightningMock(modulePath) {
51-
const p = path.join(__dirname, 'lightning-stubs', modulePath);
50+
function getModuleMock(modulePath, moduleName) {
51+
const p = path.join(__dirname, modulePath, moduleName);
5252
if (fs.existsSync(p)) {
53-
return path.join(p, modulePath + '.js');
53+
return path.join(p, moduleName + '.js');
5454
}
5555
}
5656

57+
function getLightningMock(moduleName) {
58+
return getModuleMock('lightning-stubs', moduleName);
59+
}
60+
61+
function getApexMock(moduleName) {
62+
return getModuleMock('apex-stubs', moduleName);
63+
}
64+
5765
function getModule(modulePath, options) {
5866
const { ns, name } = getInfoFromId(modulePath);
5967

6068
if (ns === 'lightning') {
6169
return getLightningMock(name);
6270
}
6371

72+
// See https://developer.salesforce.com/docs/component-library/documentation/en/lwc/reference_salesforce_modules
73+
if (
74+
modulePath.startsWith('@salesforce/apex/') ||
75+
modulePath.startsWith('@salesforce/apexContinuation/')
76+
) {
77+
return getApexMock('method');
78+
}
79+
6480
if (ns === DEFAULT_NAMESPACE) {
6581
const paths = getModulePaths();
6682
for (let i = 0; i < paths.length; i++) {

0 commit comments

Comments
 (0)