@justeattakeaway/pie-webc-core 0.9.1 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.11.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [Added] - defineCustomElement helper function to protect against duplicate component registration errors ([#905](https://github.com/justeattakeaway/pie/pull/905)) by [@jamieomaguire](https://github.com/jamieomaguire)
8
+
9
+ ## 0.10.0
10
+
11
+ ### Minor Changes
12
+
13
+ - [Fixed] - License removed from packages ([#869](https://github.com/justeattakeaway/pie/pull/869)) by [@kevinrodrigues](https://github.com/kevinrodrigues)
14
+
3
15
  ## 0.9.1
4
16
 
5
17
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@justeattakeaway/pie-webc-core",
3
- "version": "0.9.1",
3
+ "version": "0.11.0",
4
4
  "description": "PIE design system base classes, mixins and utilities for web components",
5
5
  "type": "module",
6
6
  "main": "index.ts",
@@ -0,0 +1,47 @@
1
+ import { LitElement } from 'lit';
2
+
3
+ /**
4
+ * Defines a web component, ensuring that the component is only defined once in the Custom Element Registry.
5
+ *
6
+ * If the component has already been defined with the same name, a warning is logged to the console.
7
+ *
8
+ * @param {string} elementSelector - The selector of the custom element. Must be a valid custom element selector, containing a dash. I.e. 'my-component'
9
+ * @param {typeof LitElement} elementClass - The class that defines the custom element, extending LitElement.
10
+ *
11
+ * @example
12
+ *
13
+ * ```javascript
14
+ * import { css, html, LitElement } from 'lit';
15
+ *
16
+ * // IMPORTANT: Add the following JSDoc comment above your class setting the `@tagname` to the selector you want to use. - This is mandatory.
17
+ * // "@tagname my-component"
18
+ * class MyComponent extends LitElement {
19
+ * static styles = css`
20
+ * :host {
21
+ * display: block;
22
+ * padding: 16px;
23
+ * color: var(--my-component-text-color, black);
24
+ * }
25
+ * `;
26
+ *
27
+ * render() {
28
+ * return html`
29
+ * <p>My custom element content goes here!</p>
30
+ * `;
31
+ * }
32
+ * }
33
+ *
34
+ * defineCustomElement('my-component', MyComponent);
35
+ * ```
36
+ *
37
+ * NOTE: The inclusion of the `@tagname` JSDoc comment above your class is essential for correct functionality. Ensure it matches the tag you're registering.
38
+ *
39
+ * @returns {void}
40
+ */
41
+ export function defineCustomElement (elementSelector: string, elementClass: typeof LitElement): void {
42
+ if (!customElements.get(elementSelector)) {
43
+ customElements.define(elementSelector, elementClass);
44
+ } else {
45
+ console.warn(`PIE Web Component: "${elementSelector}" has already been defined. Please ensure the component is only being defined once in your application.`);
46
+ }
47
+ }
@@ -0,0 +1 @@
1
+ export * from './defineCustomElement';
@@ -0,0 +1,81 @@
1
+ /* eslint-disable max-classes-per-file */
2
+ import { LitElement } from 'lit';
3
+ import {
4
+ vi, expect, it,
5
+ } from 'vitest';
6
+ import { defineCustomElement } from '../defineCustomElement';
7
+
8
+ it('should call console.warn when a component is defined twice', () => {
9
+ // Arrange
10
+ class MockComponentA extends LitElement {}
11
+
12
+ const warnSpy = vi.spyOn(console, 'warn');
13
+ const errorSpy = vi.spyOn(console, 'error');
14
+
15
+ // Act
16
+ defineCustomElement('mock-component-a', MockComponentA);
17
+ defineCustomElement('mock-component-a', MockComponentA);
18
+
19
+ // Assert
20
+ const [[warning]] = warnSpy.mock.calls;
21
+
22
+ expect(warnSpy).toHaveBeenCalledOnce();
23
+ expect(warning).toMatch('PIE Web Component: "mock-component-a" has already been defined. Please ensure the component is only being defined once in your application.');
24
+ expect(errorSpy).not.toHaveBeenCalled();
25
+ expect(customElements.get('mock-component-a')).toBe(MockComponentA);
26
+ });
27
+
28
+ it('should define the component without warnings when called once', () => {
29
+ // Arrange
30
+ class MockComponentB extends LitElement {}
31
+
32
+ const warnSpy = vi.spyOn(console, 'warn');
33
+ const errorSpy = vi.spyOn(console, 'error');
34
+
35
+ // Act
36
+ defineCustomElement('mock-component-b', MockComponentB);
37
+
38
+ // Assert
39
+ expect(warnSpy).not.toHaveBeenCalled();
40
+ expect(errorSpy).not.toHaveBeenCalled();
41
+ expect(customElements.get('mock-component-b')).toBe(MockComponentB);
42
+ });
43
+
44
+ it('should warn when defining the same component name with a different class', () => {
45
+ // Arrange
46
+ class MockComponentC extends LitElement {}
47
+
48
+ class MockComponentD extends LitElement {}
49
+
50
+ const warnSpy = vi.spyOn(console, 'warn');
51
+ const errorSpy = vi.spyOn(console, 'error');
52
+
53
+ // Act
54
+ defineCustomElement('mock-component-c', MockComponentC);
55
+ defineCustomElement('mock-component-c', MockComponentD);
56
+
57
+ // Assert
58
+ const [[warning]] = warnSpy.mock.calls;
59
+
60
+ expect(warnSpy).toHaveBeenCalledOnce();
61
+ expect(warning).toMatch('PIE Web Component: "mock-component-c" has already been defined. Please ensure the component is only being defined once in your application.');
62
+ expect(errorSpy).not.toHaveBeenCalled();
63
+ expect(customElements.get('mock-component-c')).toBe(MockComponentC);
64
+ });
65
+
66
+ it('Should correctly accept component classes that use multiple levels of inheritance', () => {
67
+ // Arrange
68
+ class MockComponentE extends LitElement {}
69
+ class MockComponentF extends MockComponentE {}
70
+
71
+ const warnSpy = vi.spyOn(console, 'warn');
72
+ const errorSpy = vi.spyOn(console, 'error');
73
+
74
+ // Act
75
+ defineCustomElement('mock-component-f', MockComponentF);
76
+
77
+ // Assert
78
+ expect(warnSpy).not.toHaveBeenCalled();
79
+ expect(errorSpy).not.toHaveBeenCalled();
80
+ expect(customElements.get('mock-component-f')).toBe(MockComponentF);
81
+ });
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './mixins';
2
2
  export * from './decorators';
3
+ export * from './functions';
package/LICENSE DELETED
@@ -1,17 +0,0 @@
1
- Apache License
2
- Version 2.0, January 2004
3
- http://www.apache.org/licenses/
4
-
5
- Copyright (c) Just Eat Takeaway.com
6
-
7
- Licensed under the Apache License, Version 2.0 (the "License");
8
- you may not use this file except in compliance with the License.
9
- You may obtain a copy of the License at
10
-
11
- http://www.apache.org/licenses/LICENSE-2.0
12
-
13
- Unless required by applicable law or agreed to in writing, software
14
- distributed under the License is distributed on an "AS IS" BASIS,
15
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- See the License for the specific language governing permissions and
17
- limitations under the License.