Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: add apexContinuation transformer (#1074)
  • Loading branch information
apapko committed Feb 22, 2019
1 parent d1c54a8 commit 01ce6c3
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/@lwc/jest-transformer/src/index.js
Expand Up @@ -14,6 +14,7 @@ const engineVersion = require('@lwc/engine/package.json').version;
const compilerVersion = require('@lwc/compiler/package.json').version;
const { waitForPromise } = require('./utils');
const apexScopedImport = require('./transforms/apex-scoped-import');
const apexContinuationScopedImport = require('./transforms/apex-continuation-scoped-import');
const i18nScopedImport = require('./transforms/i18n-scoped-import');
const labelScopedImport = require('./transforms/label-scoped-import');
const resourceScopedImport = require('./transforms/resource-scoped-import');
Expand All @@ -29,6 +30,7 @@ const BABEL_CONFIG = {
"plugins": [
babelCommonJs,
apexScopedImport,
apexContinuationScopedImport,
i18nScopedImport,
labelScopedImport,
contentAssetUrlScopedImport,
Expand Down
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2018, 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
*/
const test = require('./utils/test-transform').test(
require('../apex-continuation-scoped-import')
);

describe('@salesforce/apexContinuation import', () => {
test('does default transformation', `
import myMethod from '@salesforce/apexContinuation/FooController.fooMethod';
`, `
let myMethod;
try {
myMethod = require("@salesforce/apexContinuation/FooController.fooMethod").default;
} catch (e) {
global.__lwcJestMock_myMethod = global.__lwcJestMock_myMethod || function () {
return Promise.resolve();
};
myMethod = global.__lwcJestMock_myMethod;
}
`);

test('allows non-@salesforce/apexContinuation named imports', `
import { otherNamed } from './something-valid';
import myMethod from '@salesforce/apexContinuation/FooController.fooMethod';
`, `
import { otherNamed } from './something-valid';
let myMethod;
try {
myMethod = require("@salesforce/apexContinuation/FooController.fooMethod").default;
} catch (e) {
global.__lwcJestMock_myMethod = global.__lwcJestMock_myMethod || function () {
return Promise.resolve();
};
myMethod = global.__lwcJestMock_myMethod;
}
`);
});
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2018, 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
*/
const babelTemplate = require('@babel/template').default;
const { getImportInfo } = require('./utils');

const APEX_CONTINUATION_IMPORT_IDENTIFIER = '@salesforce/apexContinuation';

/*
* Apex imports can be used as @wire ids or called directly. If used as a @wire
* id, it must be the same object in the component under test and the test case
* itself. Due to this requirement, we save the mock to the global object to be
* shared.
*/
const resolvedPromiseTemplate = babelTemplate(`
let RESOURCE_NAME;
try {
RESOURCE_NAME = require(IMPORT_SOURCE).default;
} catch (e) {
global.MOCK_NAME = global.MOCK_NAME || function() { return Promise.resolve(); };
RESOURCE_NAME = global.MOCK_NAME;
}
`);

module.exports = function ({ types: t }) {
return {
visitor: {
ImportDeclaration(path) {
const { importSource, resourceNames } = getImportInfo(path, true);

if (importSource.startsWith(APEX_CONTINUATION_IMPORT_IDENTIFIER)) {
// importing anything after '@salesforce/apexContinuation' means they're getting a single Apex method as the default import
// e.g. `import myMethod from '@salesforce/apexContinuation/FooController.fooMethod';`
path.replaceWithMultiple(resolvedPromiseTemplate({
RESOURCE_NAME: t.identifier(resourceNames[0]),
IMPORT_SOURCE: t.stringLiteral(importSource),
MOCK_NAME: `__lwcJestMock_${resourceNames[0]}`,
}));
}
}
}
};
};

0 comments on commit 01ce6c3

Please sign in to comment.