From 5524c2e323eb5ce9e51b831d80a65765f0097b8d Mon Sep 17 00:00:00 2001 From: Pierre Paridans Date: Thu, 20 Jun 2019 11:38:00 +0200 Subject: [PATCH] [IMP] store: connected component reuse base component name Connected component should have a unique name. To achieve this the connected component currently have an id based generated name (like `ConnectedComponent1`). To make it clearer and ease debugging this commit reuse the extended component's name to include it in the generated name in the form of `Connected`. --- src/store.ts | 4 +--- tests/store.test.ts | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/store.ts b/src/store.ts index 21f84fd5..3a93ee18 100644 --- a/src/store.ts +++ b/src/store.ts @@ -170,8 +170,6 @@ interface StoreOptions { deep?: boolean; } -let nextID = 1; - export function connect( Comp: Constructor>, mapStoreToProps, @@ -292,7 +290,7 @@ export function connect( // this is necessary for Owl to be able to properly deduce templates. // Otherwise, all connected components would have the same name, and then // each component after the first will necessarily have the same template. - let name = `ConnectedComponent${nextID++}`; + let name = `Connected${Comp.name}`; Object.defineProperty(Result, "name", { value: name }); return Result; } diff --git a/tests/store.test.ts b/tests/store.test.ts index 592cd9fe..e6a5af88 100644 --- a/tests/store.test.ts +++ b/tests/store.test.ts @@ -1157,4 +1157,21 @@ describe("connecting a component to store", () => { expect(fixture.innerHTML).toBe("
b
"); expect(steps).toEqual(["willpatch", "patched"]); }); + + test("connected component has its own name", () => { + function mapStoreToProps() { } + + class Named extends Component { }; + const namedConnected = connect(Named, mapStoreToProps); + expect(namedConnected.name).toMatch('ConnectedNamed'); + + class ParentNamed extends Component{}; + class ChildNamed extends ParentNamed{}; + const childConnected = connect(ChildNamed, mapStoreToProps) + expect(childConnected.name).toMatch('ConnectedChildNamed') + + const Anonymous = class extends Component{ }; + const anonymousConnected = connect(Anonymous, mapStoreToProps); + expect(anonymousConnected.name).toMatch(/^Connectedclass_\d+/); + }); });