Skip to content

Commit

Permalink
fix: automock apex methods with valid wire adapters (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
jodarove committed Mar 30, 2021
1 parent 631cdef commit 125c689
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"overrides": [
{
"files": ["src/lightning-stubs/**"],
"files": ["src/lightning-stubs/**", "src/apex-stubs/**"],
"parser": "babel-eslint",
"parserOptions": {
"sourceType": "module"
Expand Down
9 changes: 9 additions & 0 deletions src/apex-stubs/method/method.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { createApexTestWireAdapter } from '@salesforce/wire-service-jest-util';

export default createApexTestWireAdapter(jest.fn());
22 changes: 19 additions & 3 deletions src/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,36 @@ function resolveAsFile(name, extensions) {
return undefined;
}

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

function getLightningMock(moduleName) {
return getModuleMock('lightning-stubs', moduleName);
}

function getApexMock(moduleName) {
return getModuleMock('apex-stubs', moduleName);
}

function getModule(modulePath, options) {
const { ns, name } = getInfoFromId(modulePath);

if (ns === 'lightning') {
return getLightningMock(name);
}

// See https://developer.salesforce.com/docs/component-library/documentation/en/lwc/reference_salesforce_modules
if (
modulePath.startsWith('@salesforce/apex/') ||
modulePath.startsWith('@salesforce/apexContinuation/')
) {
return getApexMock('method');
}

if (ns === DEFAULT_NAMESPACE) {
const paths = getModulePaths();
for (let i = 0; i < paths.length; i++) {
Expand Down

0 comments on commit 125c689

Please sign in to comment.