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: provide reset last config method #73

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
43 changes: 19 additions & 24 deletions src/adapters/ApexTestWireAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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 });
}
};
}
43 changes: 19 additions & 24 deletions src/adapters/LdsTestWireAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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 })
}
};
}
53 changes: 2 additions & 51 deletions src/adapters/TestWireAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +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;
}

_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();
}
60 changes: 60 additions & 0 deletions src/adapters/adapter.js
Original file line number Diff line number Diff line change
@@ -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();
Comment on lines +8 to +9
Copy link
Author

Choose a reason for hiding this comment

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

With this approach, we generate each time a new class and we don't expose directly the lastConfig and wireInstances variables. Then we ensure to not have some troubles with inheritances


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;
}
Comment on lines +26 to +28
Copy link
Author

Choose a reason for hiding this comment

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

The new method


_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;
}
}
}
32 changes: 27 additions & 5 deletions test/modules/example/apex/__tests__/apex-adapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,24 +201,46 @@ 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';
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}`);
});
});
});

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 });
Expand Down
30 changes: 26 additions & 4 deletions test/modules/example/generic/__tests__/test-adapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,44 @@ 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}`);
});
});
});

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 });
Expand Down
30 changes: 26 additions & 4 deletions test/modules/example/lds/__tests__/lds-adapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,22 +196,44 @@ 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}`);
});
});
});

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 });
Expand Down