Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat: add apexContinuation transformer (#1074)
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...es/@lwc/jest-transformer/src/transforms/__tests__/apex-continuation-scoped-import.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
`); | ||
}); |
46 changes: 46 additions & 0 deletions
46
packages/@lwc/jest-transformer/src/transforms/apex-continuation-scoped-import.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]}`, | ||
})); | ||
} | ||
} | ||
} | ||
}; | ||
}; |