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

chore: upgrade to latest lwc and @lwc/jest-preset #35

Merged
merged 6 commits into from
Jan 28, 2021
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
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jobs:

- restore_cache:
keys:
- v1-dependencies-{{ checksum "yarn.lock" }}
- v1-dependencies-
- v2-dependencies-{{ checksum "yarn.lock" }}
- v2-dependencies-

- run: yarn install --frozen-lock

Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
"testing"
],
"repository": "https://github.com/salesforce/wire-service-jest-util",
"peerDependencies": {
"@lwc/engine": "^1.5.0"
},
"devDependencies": {
"@lwc/compiler": "^1.3.2",
"@lwc/engine": "^1.3.2",
"@lwc/jest-transformer": "^4.5.1",
"@lwc/jest-preset": "^4.5.1",
"lwc": "^1.3.2",
"@lwc/wire-service": "^1.3.2",
"@lwc/compiler": "^1.11.3",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we drop support for @lwc/engine < 1.5, I would recommend adding a peer dependency restriction. This would print a warning on installation if the condition is not met.

"@lwc/engine": "^1.11.3",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"@lwc/synthetic-shadow": "^1.11.3",
"@lwc/jest-preset": "^9.0.0",
"@salesforce/eslint-config-lwc": "^0.5.0",
"eslint": "^6.8.0",
"isbinaryfile": "^4.0.6",
Expand Down
29 changes: 1 addition & 28 deletions src/adapters/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,12 @@
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { register, ValueChangedEvent } from '@lwc/wire-service';

const wireAdaptersRegistryHack = global.wireAdaptersRegistryHack || new Map();

function createLegacyAdapter(adapterId, spy) {
register(adapterId, (wiredEventTarget) => {
const instance = {
emit(value) {
wiredEventTarget.dispatchEvent(new ValueChangedEvent(value));
}
};
spy.createInstance(instance);

wiredEventTarget.addEventListener('connect', () => {
spy.connect(instance);
});

wiredEventTarget.addEventListener('disconnect', () => {
spy.disconnect(instance);
});

wiredEventTarget.addEventListener('config', (newConfig) => {
spy.update(instance, newConfig);
});
});
}

export function spyOnAdapter(spy, adapterId) {
const relatedAdapter = wireAdaptersRegistryHack.get(adapterId);

if (!relatedAdapter) {
createLegacyAdapter(adapterId, spy);
} else {
if (relatedAdapter) {
relatedAdapter.adapter.spyAdapter(spy);
}
}
19 changes: 0 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,10 @@
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { register } from '@lwc/engine';
import { registerWireService } from '@lwc/wire-service';
import createLdsAdapter from './adapters/lds';
import createGenericAdapter from './adapters/generic';
import createApexAdapter from './adapters/apex';

let registered = false;

// Not needed since lwc >= 1.5.0
function ensureWireServiceRegistered() {
if (!registered) {
registerWireService(register);
registered = true;
}
}


function validateAdapterId(adapterId) {
if (!adapterId) {
throw new Error('No adapter specified');
Expand All @@ -30,24 +17,18 @@ function validateAdapterId(adapterId) {
function registerLdsTestWireAdapter(identifier) {
validateAdapterId(identifier);

ensureWireServiceRegistered();

return createLdsAdapter(identifier);
}

function registerApexTestWireAdapter(identifier) {
validateAdapterId(identifier);

ensureWireServiceRegistered();

return createApexAdapter(identifier);
}

function registerTestWireAdapter(identifier) {
validateAdapterId(identifier);

ensureWireServiceRegistered();

return createGenericAdapter(identifier);
}

Expand Down
7 changes: 4 additions & 3 deletions test/modules/example/apex/__tests__/apex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ describe('example-apex', () => {
describe('getLastConfig()', () => {
it('should return last available config', () => {
const element = createElement('example-apex', { is: Apex });
element.param = 'v1';
document.body.appendChild(element);

return Promise.resolve()
.then(() => {
expect(apexMethodAdapter.getLastConfig()).toStrictEqual({});
expect(apexMethodAdapter.getLastConfig()).toStrictEqual({ p: 'v1'});

element.param = 'v1';
element.param = 'v2';
})
.then(() => {
expect(apexMethodAdapter.getLastConfig()).toStrictEqual({ p: "v1" });
expect(apexMethodAdapter.getLastConfig()).toStrictEqual({ p: 'v2' });
});
});
});
Expand Down
7 changes: 4 additions & 3 deletions test/modules/example/generic/__tests__/generic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ describe('example-generic', () => {
describe('getLastConfig()', () => {
it('should return last available config', () => {
const element = createElement('example-generic', { is: Generic });
element.param = 'v1';
document.body.appendChild(element);

return Promise.resolve()
.then(() => {
expect(testWireAdapter.getLastConfig()).toStrictEqual({});
expect(testWireAdapter.getLastConfig()).toStrictEqual({ p: 'v1' });

element.param = 'v1';
element.param = 'v2';
})
.then(() => {
expect(testWireAdapter.getLastConfig()).toStrictEqual({ p: "v1" });
expect(testWireAdapter.getLastConfig()).toStrictEqual({ p: 'v2' });
});
});
});
Expand Down
7 changes: 4 additions & 3 deletions test/modules/example/lds/__tests__/lds.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ describe('example-lds', () => {
describe('getLastConfig()', () => {
it('should return last available config', () => {
const element = createElement('example-lds', { is: Lds });
element.param = 'v1';
document.body.appendChild(element);

return Promise.resolve()
.then(() => {
expect(ldsTestWireAdapter.getLastConfig()).toStrictEqual({});
expect(ldsTestWireAdapter.getLastConfig()).toStrictEqual({ p: 'v1'});

element.param = 'v1';
element.param = 'v2';
})
.then(() => {
expect(ldsTestWireAdapter.getLastConfig()).toStrictEqual({ p: "v1" });
expect(ldsTestWireAdapter.getLastConfig()).toStrictEqual({ p: 'v2' });
});
});
});
Expand Down
Loading