Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add apexContinuation transformer #1074

Merged
merged 1 commit into from
Feb 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/@lwc/jest-transformer/src/index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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;
}
`);
});
Original file line number Diff line number Diff line change
@@ -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]}`,
}));
}
}
}
};
};