This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lf/message server graceful error (#267)
* fix: lms new message * fix: using a real regexp * fix: adding test file * fix: tsc and lint errors
- Loading branch information
1 parent
f15c90e
commit 3180aa8
Showing
7 changed files
with
112 additions
and
5 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
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
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
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
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,41 @@ | ||
import { CompileService } from '@webruntime/api'; | ||
import path from 'path'; | ||
|
||
const APEX_LWC_MESSAGE_SERVICE_REGEX = /^(@salesforce\/messageChannel\/.*)/; | ||
|
||
function matchesLWCMessageServiceScopedModule(id: string) { | ||
return id && id.match(APEX_LWC_MESSAGE_SERVICE_REGEX); | ||
} | ||
|
||
/** | ||
* Rollup plugin to resolve @salesforce/messageChannel | ||
* | ||
*/ | ||
function plugin() { | ||
return { | ||
name: 'rollup-plugin-lwc-message-service', | ||
|
||
resolveId(id: string) { | ||
return matchesLWCMessageServiceScopedModule(id) ? id : null; | ||
}, | ||
load(id: any) { | ||
const idParts = matchesLWCMessageServiceScopedModule(id); | ||
if (idParts) { | ||
throw new Error( | ||
"You can't preview a component using Lightning Message Service. Test the component directly in the org" | ||
); | ||
} | ||
return null; | ||
} | ||
}; | ||
} | ||
|
||
export class LWCMessageService implements CompileService { | ||
getPlugin() { | ||
return plugin(); | ||
} | ||
|
||
async initialize() {} | ||
|
||
shutdown() {} | ||
} |
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,61 @@ | ||
import { LWCMessageService } from '../LWCMessageService'; | ||
import { getLatestVersion } from '@webruntime/server/dist/commonjs/utils/utils'; | ||
import { PublicConfig } from '@webruntime/api'; | ||
|
||
jest.mock('@webruntime/server/dist/commonjs/utils/utils'); | ||
|
||
describe('LWCMessageService', () => { | ||
let config: PublicConfig; | ||
|
||
beforeEach(() => { | ||
(getLatestVersion as jest.Mock).mockReturnValue('158104e2eb'); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('returns a rollup plugin', () => { | ||
const service = new LWCMessageService(); | ||
expect(service.getPlugin()).toBeTruthy(); | ||
}); | ||
|
||
describe('rollup plugin', () => { | ||
describe('resolveId', () => { | ||
it('should resolve paths with @salesforce/messageChannel', () => { | ||
const service = new LWCMessageService(); | ||
const plugin = service.getPlugin(); | ||
|
||
const resolved = plugin.resolveId( | ||
'@salesforce/messageChannel/SampleMessageChannel__c' | ||
); | ||
expect(resolved).toBe( | ||
'@salesforce/messageChannel/SampleMessageChannel__c' | ||
); | ||
}); | ||
}); | ||
|
||
describe('load', () => { | ||
it('should throw error for @salesforce/messageChannel imports', () => { | ||
(getLatestVersion as jest.Mock).mockReturnValue('158104e2eb'); | ||
|
||
const service = new LWCMessageService(); | ||
const plugin = service.getPlugin(); | ||
function loadApexContinuation() { | ||
plugin.load( | ||
'@salesforce/messageChannel/SampleMessageChannel__c' | ||
); | ||
} | ||
expect(loadApexContinuation).toThrowErrorMatchingSnapshot(); | ||
}); | ||
|
||
it('should return null instead of throwing error for other than @salesforce/messageChannel imports', () => { | ||
const service = new LWCMessageService(); | ||
const plugin = service.getPlugin(); | ||
const resolved = plugin.load('./lightningMessageService.html'); | ||
|
||
expect(resolved).toBeNull(); | ||
}); | ||
}); | ||
}); | ||
}); |
3 changes: 3 additions & 0 deletions
3
src/server/services/__tests__/__snapshots__/LWCMessageService.test.ts.snap
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,3 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`LWCMessageService rollup plugin load should throw error for @salesforce/messageChannel imports 1`] = `"You can't preview a component using Lightning Message Service. Test the component directly in the org"`; |