From fdf42e41722230e498829a3d037df351e1c88cfe Mon Sep 17 00:00:00 2001 From: Julien Roche Date: Mon, 20 Feb 2023 14:17:37 +0100 Subject: [PATCH 1/5] feat(TestWireAdapter): expose a resetLastConfig method --- src/adapters/TestWireAdapter.js | 4 ++++ .../generic/__tests__/test-adapter.test.js | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/adapters/TestWireAdapter.js b/src/adapters/TestWireAdapter.js index 5b84994..3491424 100644 --- a/src/adapters/TestWireAdapter.js +++ b/src/adapters/TestWireAdapter.js @@ -22,6 +22,10 @@ export class TestWireAdapterTemplate { return this._lastConfig; } + static resetLastConfig() { + this._lastConfig = null; + } + _dataCallback; config = {}; diff --git a/test/modules/example/generic/__tests__/test-adapter.test.js b/test/modules/example/generic/__tests__/test-adapter.test.js index 3f69139..6212e8b 100644 --- a/test/modules/example/generic/__tests__/test-adapter.test.js +++ b/test/modules/example/generic/__tests__/test-adapter.test.js @@ -101,6 +101,28 @@ describe('TestWireAdapter', () => { }); }); + describe('resetLastConfig', () => { + test('getLastConfig() should return the last available config despite we run a new set of test', () => { + //Arrange + const actual = adapter.getLastConfig(); + + // Assert + expect(actual).not.toBeNull(); + }); + + it('should reset the last available', () => { + // Arrange + let actual; + + //Act + adapter.resetLastConfig(); + actual = adapter.getLastConfig(); + + // Assert + expect(actual).toBeNull(); + }); + }); + describe('emit()', () => { it('should emit value when component is created but not connected', () => { const element = createElement('example-generic', { is: Generic }); From b7151c2d44afac2b08d2846523a617c9b5988544 Mon Sep 17 00:00:00 2001 From: Julien Roche Date: Mon, 20 Feb 2023 14:25:00 +0100 Subject: [PATCH 2/5] test(LdsTestWireAdapter): ensure ti works fine for LDS --- .../example/lds/__tests__/lds-adapter.test.js | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/modules/example/lds/__tests__/lds-adapter.test.js b/test/modules/example/lds/__tests__/lds-adapter.test.js index 8474093..4730149 100644 --- a/test/modules/example/lds/__tests__/lds-adapter.test.js +++ b/test/modules/example/lds/__tests__/lds-adapter.test.js @@ -212,6 +212,28 @@ describe('LdsTestWireAdapter', () => { }); }); + describe('resetLastConfig', () => { + test('getLastConfig() should return the last available config despite we run a new set of test', () => { + //Arrange + const actual = adapter.getLastConfig(); + + // Assert + expect(actual).not.toBeNull(); + }); + + it('should reset the last available', () => { + // Arrange + let actual; + + //Act + adapter.resetLastConfig(); + actual = adapter.getLastConfig(); + + // Assert + expect(actual).toBeNull(); + }); + }); + describe('emit()', () => { it('should emit default value when component is created but not connected', () => { const element = createElement('example-lds', { is: Lds }); From bbe6ee0f9ab45ced61949efb8fb1de69bf1aa2fa Mon Sep 17 00:00:00 2001 From: Julien Roche Date: Mon, 20 Feb 2023 14:40:32 +0100 Subject: [PATCH 3/5] test(getLAstConfig): ensure we retrieve the expect value over jest.forEach --- test/modules/example/apex/__tests__/apex-adapter.test.js | 8 ++++---- .../example/generic/__tests__/test-adapter.test.js | 8 ++++---- test/modules/example/lds/__tests__/lds-adapter.test.js | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/test/modules/example/apex/__tests__/apex-adapter.test.js b/test/modules/example/apex/__tests__/apex-adapter.test.js index 97e7e6f..54b2810 100644 --- a/test/modules/example/apex/__tests__/apex-adapter.test.js +++ b/test/modules/example/apex/__tests__/apex-adapter.test.js @@ -203,18 +203,18 @@ describe('ApexTestWireAdapter', () => { describe('getLastConfig()', () => { it('should return last available config', () => { const element = createElement('example-apex', { is: Apex }); - element.param = 'v1'; + element.param = `v1-${adapterName}`; document.body.appendChild(element); return Promise.resolve() .then(() => { - expect(adapter.getLastConfig().p).toBe('v1'); + expect(adapter.getLastConfig().p).toBe(`v1-${adapterName}`); - element.param = 'v2'; + element.param = `v2-${adapterName}`; }) .then(() => { - expect(adapter.getLastConfig().p).toBe('v2'); + expect(adapter.getLastConfig().p).toBe(`v2-${adapterName}`); }); }); }); diff --git a/test/modules/example/generic/__tests__/test-adapter.test.js b/test/modules/example/generic/__tests__/test-adapter.test.js index 6212e8b..fc68d31 100644 --- a/test/modules/example/generic/__tests__/test-adapter.test.js +++ b/test/modules/example/generic/__tests__/test-adapter.test.js @@ -85,18 +85,18 @@ describe('TestWireAdapter', () => { describe('getLastConfig()', () => { it('should return last available config', () => { const element = createElement('example-generic', { is: Generic }); - element.param = 'v1'; + element.param = `v1-${adapterName}`; document.body.appendChild(element); return Promise.resolve() .then(() => { - expect(adapter.getLastConfig().p).toBe('v1'); + expect(adapter.getLastConfig().p).toBe(`v1-${adapterName}`); - element.param = 'v2'; + element.param = `v2-${adapterName}`; }) .then(() => { - expect(adapter.getLastConfig().p).toBe('v2'); + expect(adapter.getLastConfig().p).toBe(`v2-${adapterName}`); }); }); }); diff --git a/test/modules/example/lds/__tests__/lds-adapter.test.js b/test/modules/example/lds/__tests__/lds-adapter.test.js index 4730149..7805ecc 100644 --- a/test/modules/example/lds/__tests__/lds-adapter.test.js +++ b/test/modules/example/lds/__tests__/lds-adapter.test.js @@ -196,18 +196,18 @@ describe('LdsTestWireAdapter', () => { describe('getLastConfig()', () => { it('should return last available config', () => { const element = createElement('example-lds', { is: Lds }); - element.param = 'v1'; + element.param = `v1-${adapterName}`; document.body.appendChild(element); return Promise.resolve() .then(() => { - expect(adapter.getLastConfig().p).toStrictEqual('v1'); + expect(adapter.getLastConfig().p).toStrictEqual(`v1-${adapterName}`); - element.param = 'v2'; + element.param = `v2-${adapterName}`; }) .then(() => { - expect(adapter.getLastConfig().p).toStrictEqual('v2'); + expect(adapter.getLastConfig().p).toStrictEqual(`v2-${adapterName}`); }); }); }); From 6e3909f00ca8176dc4ac6bd76c05038cacc7ef6b Mon Sep 17 00:00:00 2001 From: Julien Roche Date: Mon, 20 Feb 2023 16:08:40 +0100 Subject: [PATCH 4/5] test(TestWireAdapter) add test for Apex adapter --- .../apex/__tests__/apex-adapter.test.js | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/test/modules/example/apex/__tests__/apex-adapter.test.js b/test/modules/example/apex/__tests__/apex-adapter.test.js index 54b2810..e9984a2 100644 --- a/test/modules/example/apex/__tests__/apex-adapter.test.js +++ b/test/modules/example/apex/__tests__/apex-adapter.test.js @@ -201,7 +201,7 @@ describe('ApexTestWireAdapter', () => { cases.forEach(({ testName, adapter, adapterName}) => { describe(testName, ()=> { describe('getLastConfig()', () => { - it('should return last available config', () => { + it('should return last available config', async () => { const element = createElement('example-apex', { is: Apex }); element.param = `v1-${adapterName}`; @@ -219,6 +219,28 @@ describe('ApexTestWireAdapter', () => { }); }); + describe('resetLastConfig', () => { + test('getLastConfig() should return the last available config despite we run a new set of test', () => { + //Arrange + const actual = adapter.getLastConfig(); + + // Assert + expect(actual).not.toBeNull(); + }); + + it('should reset the last available', () => { + // Arrange + let actual; + + //Act + adapter.resetLastConfig(); + actual = adapter.getLastConfig(); + + // Assert + expect(actual).toBeNull(); + }); + }); + describe('emit()', () => { it('should emit default value when component is created but not connected', () => { const element = createElement('example-apex', { is: Apex }); From 3b87e2d4fa6f84fbd3522858e47ee554ddfcedb8 Mon Sep 17 00:00:00 2001 From: Julien Roche Date: Tue, 21 Feb 2023 09:37:07 +0100 Subject: [PATCH 5/5] refactor(adapter): put a cleaner way for inheritance --- src/adapters/ApexTestWireAdapter.js | 43 +++++++++------------ src/adapters/LdsTestWireAdapter.js | 43 +++++++++------------ src/adapters/TestWireAdapter.js | 57 +-------------------------- src/adapters/adapter.js | 60 +++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 103 deletions(-) create mode 100644 src/adapters/adapter.js diff --git a/src/adapters/ApexTestWireAdapter.js b/src/adapters/ApexTestWireAdapter.js index 34d2487..2e015a7 100644 --- a/src/adapters/ApexTestWireAdapter.js +++ b/src/adapters/ApexTestWireAdapter.js @@ -5,7 +5,7 @@ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ -import { TestWireAdapterTemplate } from "./TestWireAdapter"; +import { generateAdapter } from "./adapter"; function buildErrorObject({ body, status, statusText }) { if (status && (status < 400 || status > 599)) { @@ -28,33 +28,28 @@ function buildErrorObject({ body, status, statusText }) { }; } -class ApexTestWireAdapterTemplate extends TestWireAdapterTemplate { - static emit(value, filterFn) { - super.emit({ data: value, error: undefined }, filterFn); - } +export function buildApexTestWireAdapter() { + return class ApexTestWireAdapter extends generateAdapter() { + static emit(value, filterFn) { + super.emit({ data: value, error: undefined }, filterFn); + } - static emitError(errorOptions, filterFn) { - const err = buildErrorObject(errorOptions || {}); + static emitError(errorOptions, filterFn) { + const err = buildErrorObject(errorOptions || {}); - super.emit({ data: undefined, error: err }, filterFn); - } + super.emit({ data: undefined, error: err }, filterFn); + } - static error(body, status, statusText) { - const err = buildErrorObject({ body, status, statusText }); - - super.emit({ data: undefined, error: err }); - } + static error(body, status, statusText) { + const err = buildErrorObject({ body, status, statusText }); - constructor(dataCallback) { - super(dataCallback); + super.emit({ data: undefined, error: err }); + } - this.emit({ data: undefined, error: undefined }); - } -} + constructor(dataCallback) { + super(dataCallback); -export function buildApexTestWireAdapter() { - return class ApexTestWireAdapter extends ApexTestWireAdapterTemplate { - static _lastConfig = null; - static _wireInstances = new Set(); - } + this.emit({ data: undefined, error: undefined }); + } + }; } \ No newline at end of file diff --git a/src/adapters/LdsTestWireAdapter.js b/src/adapters/LdsTestWireAdapter.js index 0529005..6b17e6e 100644 --- a/src/adapters/LdsTestWireAdapter.js +++ b/src/adapters/LdsTestWireAdapter.js @@ -5,7 +5,7 @@ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ -import { TestWireAdapterTemplate } from "./TestWireAdapter"; +import { generateAdapter } from "./adapter"; function buildErrorObject({ body, status, statusText }) { if (status && (status < 400 || status > 599)) { @@ -29,33 +29,28 @@ function buildErrorObject({ body, status, statusText }) { }; } -class LdsTestWireAdapterTemplate extends TestWireAdapterTemplate { - static emit(value, filterFn) { - super.emit({ data: value, error: undefined }, filterFn); - } +export function buildLdsTestWireAdapter() { + return class LdsTestWireAdapter extends generateAdapter() { + static emit(value, filterFn) { + super.emit({ data: value, error: undefined }, filterFn); + } - static emitError(errorOptions, filterFn) { - const err = buildErrorObject(errorOptions || {}); + static emitError(errorOptions, filterFn) { + const err = buildErrorObject(errorOptions || {}); - super.emit({ data: undefined, error: err }, filterFn); - } + super.emit({ data: undefined, error: err }, filterFn); + } - static error(body, status, statusText) { - const err = buildErrorObject({ body, status, statusText }); - - super.emit({ data: undefined, error: err }); - } + static error(body, status, statusText) { + const err = buildErrorObject({ body, status, statusText }); - constructor(dataCallback) { - super(dataCallback); + super.emit({ data: undefined, error: err }); + } - this.emit({ data: undefined, error: undefined }) - } -} + constructor(dataCallback) { + super(dataCallback); -export function buildLdsTestWireAdapter() { - return class LdsTestWireAdapter extends LdsTestWireAdapterTemplate { - static _lastConfig = null; - static _wireInstances = new Set(); - } + this.emit({ data: undefined, error: undefined }) + } + }; } \ No newline at end of file diff --git a/src/adapters/TestWireAdapter.js b/src/adapters/TestWireAdapter.js index 3491424..0e93be4 100644 --- a/src/adapters/TestWireAdapter.js +++ b/src/adapters/TestWireAdapter.js @@ -4,62 +4,9 @@ * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ -export class TestWireAdapterTemplate { - static _lastConfig = null; - static _wireInstances = new Set(); - static emit(value, filterFn) { - let instances = Array.from(this._wireInstances); - - if (typeof filterFn === 'function') { - instances = instances.filter((instance) => filterFn(instance.getConfig())); - } - - instances.forEach((instance) => instance.emit(value)); - } - - static getLastConfig() { - return this._lastConfig; - } - - static resetLastConfig() { - this._lastConfig = null; - } - - _dataCallback; - config = {}; - - constructor(dataCallback) { - this._dataCallback = dataCallback; - this.constructor._wireInstances.add(this); - } - - update(config) { - this.config = config; - this.constructor._lastConfig = config; - } - - connect() { - this.constructor._lastConfig = {}; - this.constructor._wireInstances.add(this); - } - - disconnect() { - this.constructor._wireInstances.delete(this); - } - - emit(value) { - this._dataCallback(value); - } - - getConfig() { - return this.config; - } -} +import { generateAdapter } from "./adapter"; export function buildTestWireAdapter() { - return class TestWireAdapter extends TestWireAdapterTemplate { - static _lastConfig = null; - static _wireInstances = new Set(); - } + return generateAdapter(); } diff --git a/src/adapters/adapter.js b/src/adapters/adapter.js new file mode 100644 index 0000000..3faf8f2 --- /dev/null +++ b/src/adapters/adapter.js @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2023, 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 + */ +export function generateAdapter() { + let lastConfig = null; + const wireInstances = new Set(); + + return class TestWireAdapterTemplate { + static emit(value, filterFn) { + let instances = Array.from(wireInstances); + + if (typeof filterFn === 'function') { + instances = instances.filter((instance) => filterFn(instance.getConfig())); + } + + instances.forEach((instance) => instance.emit(value)); + } + + static getLastConfig() { + return lastConfig; + } + + static resetLastConfig() { + lastConfig = null; + } + + _dataCallback; + config = {}; + + constructor(dataCallback) { + this._dataCallback = dataCallback; + wireInstances.add(this); + } + + update(config) { + this.config = config; + lastConfig = config; + } + + connect() { + lastConfig = {}; + wireInstances.add(this); + } + + disconnect() { + wireInstances.delete(this); + } + + emit(value) { + this._dataCallback(value); + } + + getConfig() { + return this.config; + } + } +} \ No newline at end of file