Skip to content

Commit 4b79471

Browse files
committed
fix build; add tests
1 parent 8f2aa2c commit 4b79471

File tree

5 files changed

+24
-23
lines changed

5 files changed

+24
-23
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# <custom-element> element
1+
# <date-time> element
22

3-
Boilerplate for creating a custom element.
3+
A custom element that displays date and time.
44

55
## Installation
66
Available on [npm](https://www.npmjs.com/) as [**@github/custom-element-element**](https://www.npmjs.com/package/@github/custom-element-element).

examples/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ <h3>&lt;date-time&gt; Custom Element</h3>
5656
Made with <span style="color: #e25555;font-size: medium;">&hearts;</span> by
5757
<a href="https://github.com/Architrixs/date-time-custom-element" target="_blank">Architrixs</a>
5858
</p>
59-
<script type="module">
60-
import '../src/date-time.ts'
59+
<script type="module"
60+
src= '../dist/src/date-time.js'>
6161
</script>
6262
</body>
6363

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"scripts": {
1515
"clean": "rm -rf dist",
1616
"lint": "eslint . --ext .ts,.js",
17-
"prebuild": "npm run clean && mkdir dist",
17+
"prebuild": "npm run clean && npm run lint && mkdir dist",
1818
"build": "tsc",
1919
"pretest": "npm run build",
2020
"test": "web-test-runner \"test/**/*\" --node-resolve",

src/date-time.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88

99
//import { DateTime } from './current-time';
1010

11-
// Define the custom element
12-
//customElements.define('current-time', DateTime);
11+
class DateTimeElement extends HTMLElement {
12+
// Attach a shadow root to the custom element
13+
#shadowRoot = this.attachShadow({mode: 'open'})
1314

14-
// The class for the custom element
15-
export class DateTimeElement extends HTMLElement {
16-
public shadowRoot: ShadowRoot
1715
private dateElement: HTMLElement
1816
private timeElement: HTMLElement
1917
private culture = 'en-US'
@@ -23,15 +21,12 @@ export class DateTimeElement extends HTMLElement {
2321
static get observedAttributes(): string[] {
2422
return ['date-format', 'time-format', 'culture']
2523
}
24+
// eslint-disable-next-line custom-elements/no-constructor
2625
constructor() {
2726
super()
28-
29-
// Attach a shadow root to the custom element
30-
this.shadowRoot = this.attachShadow({mode: 'open'})
31-
3227
// Add the template to the shadow root
3328
const template = document.createElement('template')
34-
29+
// eslint-disable-next-line github/no-inner-html
3530
template.innerHTML = `
3631
<style>
3732
#time-container {
@@ -50,11 +45,11 @@ export class DateTimeElement extends HTMLElement {
5045
<p id="time"></p>
5146
</div>
5247
`
53-
this.shadowRoot.appendChild(template.content.cloneNode(true))
48+
this.#shadowRoot.appendChild(template.content.cloneNode(true))
5449

5550
// Get the date and time elements
56-
this.dateElement = this.shadowRoot.querySelector('#date')!
57-
this.timeElement = this.shadowRoot.querySelector('#time')!
51+
this.dateElement = this.#shadowRoot.querySelector('#date')!
52+
this.timeElement = this.#shadowRoot.querySelector('#time')!
5853
}
5954

6055
// Update the element when the format attribute changes

test/test.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import {assert} from '@open-wc/testing'
2-
import {DateTimeElement} from '../src/date-time'
2+
import DateTimeElement from '../src/date-time'
33

4-
// TODO: Add tests for your element
54
describe('date-time', () => {
6-
describe('is defined', () => {
7-
const el = document.createElement('date-time')
8-
assert.instanceOf(el, DateTimeElement)
5+
describe('element creation', () => {
6+
it('creates an instance of the element from document.createElement', () => {
7+
const el = document.createElement('date-time')
8+
assert.instanceOf(el, DateTimeElement)
9+
})
10+
11+
it('creates an instance of the element from constructor', () => {
12+
const el = new DateTimeElement()
13+
assert.instanceOf(el, DateTimeElement)
14+
})
915
})
1016
})

0 commit comments

Comments
 (0)