nanosplash 3.0.0 → 3.0.2
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/.github/workflows/ci.yml +50 -44
- package/.github/workflows/vitepress.yml +69 -0
- package/.prettierignore +1 -0
- package/.prettierrc +10 -10
- package/LICENSE +21 -0
- package/README.md +14 -41
- package/bun.lockb +0 -0
- package/coverage/DomUtilities.ts.html +568 -0
- package/coverage/Service.ts.html +718 -0
- package/coverage/Splash.ts.html +448 -0
- package/coverage/SplashQueue.ts.html +232 -0
- package/coverage/base.css +224 -0
- package/coverage/block-navigation.js +87 -0
- package/coverage/clover.xml +560 -0
- package/coverage/coverage-final.json +5 -0
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +161 -0
- package/coverage/prettify.css +1 -0
- package/coverage/prettify.js +2 -0
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +196 -0
- package/dist/cjs/ns.cjs +2 -0
- package/dist/es/ns.js +342 -0
- package/dist/iife/ns.iife.js +2 -0
- package/docs/.vitepress/config.ts +75 -0
- package/docs/.vitepress/theme/css/style.css +157 -0
- package/docs/.vitepress/theme/index.ts +16 -0
- package/docs/.vitepress/theme/vue/Card.vue +31 -0
- package/docs/.vitepress/theme/vue/CardGrid.vue +17 -0
- package/docs/.vitepress/theme/vue/Exe.vue +86 -0
- package/docs/.vitepress/theme/vue/PlayGround.vue +50 -0
- package/docs/api/doc/hide.md +69 -0
- package/docs/api/doc/show.md +47 -0
- package/docs/api/start/features.md +17 -0
- package/docs/api/start/install.md +53 -0
- package/docs/api/start/playground.md +11 -0
- package/docs/api/start/usage.md +25 -0
- package/docs/index.md +30 -0
- package/package.json +86 -66
- package/src/style/ns.sass +95 -0
- package/src/ts/core/DomUtilities.ts +161 -0
- package/src/ts/core/Service.ts +211 -0
- package/src/ts/core/{Nanosplash/interfaces/NanosplashAPI.ts → ServiceInterface.ts} +18 -10
- package/src/ts/core/Splash.ts +121 -0
- package/src/ts/core/SplashInterface.ts +46 -0
- package/src/ts/core/SplashQueue.ts +49 -0
- package/src/ts/main.ts +9 -0
- package/src/ts/types/Types.ts +3 -0
- package/src/ts/types/global.d.ts +14 -0
- package/src/vite-env.d.ts +0 -8
- package/tests/DomUtilities.spec.ts +117 -0
- package/tests/Guid.spec.ts +5 -5
- package/tests/Service.spec.ts +198 -0
- package/tests/Splash.spec.ts +64 -0
- package/tests/TestUtilities.ts +48 -0
- package/tsconfig.json +21 -20
- package/vite.config.ts +14 -22
- package/dist/main.css +0 -1
- package/dist/ns.cjs.js +0 -1
- package/dist/ns.es.js +0 -330
- package/dist/ns.iife.js +0 -2
- package/index.html +0 -52
- package/src/main.ts +0 -8
- package/src/sass/ns.sass +0 -86
- package/src/ts/core/Nanosplash/Nanosplash.ts +0 -97
- package/src/ts/core/Nanosplash/interfaces/NanosplashInterface.ts +0 -42
- package/src/ts/core/Nanosplash/repositories/NanosplashRepository.ts +0 -178
- package/src/ts/core/Nanosplash/services/NanosplashService.ts +0 -222
- package/src/ts/types/Alias.d.ts +0 -1
- package/src/ts/types/Nanosplash.d.ts +0 -18
- package/src/ts/types/NanosplashService.d.ts +0 -1
- package/src/ts/util/Guid.ts +0 -13
- package/src/ts/util/Stack.ts +0 -73
- package/tests/Nanosplash.spec.ts +0 -64
- package/tests/NanosplashRepository.spec.ts +0 -91
- package/tests/NanosplashService.spec.ts +0 -119
- package/tests/Stack.spec.ts +0 -56
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { GUIDString } from '../types/Types'
|
|
2
|
+
import { createElement, showElement, hideElement } from './DomUtilities'
|
|
3
|
+
import { SplashInterface } from './SplashInterface'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* # Splash
|
|
7
|
+
* Small splash screen that can be used to indicate that a process
|
|
8
|
+
* is running.
|
|
9
|
+
* @author Isak K. Hauge <isakhauge@icloud.com>
|
|
10
|
+
*/
|
|
11
|
+
export class Splash implements SplashInterface {
|
|
12
|
+
/**
|
|
13
|
+
* # CSS Class Name
|
|
14
|
+
* The main CSS class name of the root element of a Nanosplash component.
|
|
15
|
+
*/
|
|
16
|
+
public static readonly NSClass = 'ns'
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* # Host CSS Class Name
|
|
20
|
+
* The CSS class name of the host element of a Nanosplash component.
|
|
21
|
+
* The host element is the element that the Nanosplash is attached to.
|
|
22
|
+
*/
|
|
23
|
+
public static readonly NSHostClass = 'nsh'
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* # ID
|
|
27
|
+
* Each Nanosplash instance is given a unique GUID.
|
|
28
|
+
*/
|
|
29
|
+
private id: GUIDString | null
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* # Element
|
|
33
|
+
* The root element of the Nanosplash component.
|
|
34
|
+
*/
|
|
35
|
+
private element: HTMLDivElement | null
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* # Generate GUID
|
|
39
|
+
* @returns {GUIDString} A GUID string.
|
|
40
|
+
* @private
|
|
41
|
+
*/
|
|
42
|
+
private static generateGUID(): GUIDString {
|
|
43
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
|
|
44
|
+
const r = (Math.random() * 16) | 0
|
|
45
|
+
const v = c === 'x' ? r : (r & 0x3) | 0x8
|
|
46
|
+
return v.toString(16)
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* # Constructor
|
|
52
|
+
* Creates a new Nanosplash instance.
|
|
53
|
+
*/
|
|
54
|
+
public constructor() {
|
|
55
|
+
this.element = createElement()
|
|
56
|
+
this.element.id = this.id = Splash.generateGUID()
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @inheritdoc
|
|
61
|
+
*/
|
|
62
|
+
public getId(): GUIDString | null {
|
|
63
|
+
return this.id
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @inheritdoc
|
|
68
|
+
*/
|
|
69
|
+
public getElement(): HTMLDivElement | null {
|
|
70
|
+
return <HTMLDivElement>this.element
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* @inheritdoc
|
|
75
|
+
*/
|
|
76
|
+
public getTextElement(): HTMLDivElement {
|
|
77
|
+
return (
|
|
78
|
+
<HTMLDivElement>(
|
|
79
|
+
this.getElement()?.firstElementChild?.firstElementChild
|
|
80
|
+
) ?? null
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @inheritdoc
|
|
86
|
+
*/
|
|
87
|
+
public setText(text: string): Splash {
|
|
88
|
+
this.getTextElement().innerText = text
|
|
89
|
+
text.length > 0 ? this.showText() : this.hideText()
|
|
90
|
+
return this
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @inheritdoc
|
|
95
|
+
*/
|
|
96
|
+
public showText(): Splash {
|
|
97
|
+
showElement(this.getTextElement())
|
|
98
|
+
return this
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @inheritdoc
|
|
103
|
+
*/
|
|
104
|
+
public hideText(): Splash {
|
|
105
|
+
hideElement(this.getTextElement())
|
|
106
|
+
return this
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* @inheritdoc
|
|
111
|
+
*/
|
|
112
|
+
public delete(): boolean {
|
|
113
|
+
this.id = null
|
|
114
|
+
if (this.element !== null) {
|
|
115
|
+
this.element.innerHTML = ''
|
|
116
|
+
this.element.remove()
|
|
117
|
+
this.element = null
|
|
118
|
+
}
|
|
119
|
+
return this.element === null
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { GUIDString } from '../types/Types'
|
|
2
|
+
import { Splash } from './Splash'
|
|
3
|
+
|
|
4
|
+
export interface SplashInterface {
|
|
5
|
+
/**
|
|
6
|
+
* # Get ID
|
|
7
|
+
* Return Nanosplash instance GUID.
|
|
8
|
+
*/
|
|
9
|
+
getId(): GUIDString | null
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* # Get Element
|
|
13
|
+
* Return Nanosplash instance HTMLDivElement.
|
|
14
|
+
*/
|
|
15
|
+
getElement(): HTMLDivElement | null
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* # Get Text Element
|
|
19
|
+
* Returns the text element of the Nanosplash.
|
|
20
|
+
*/
|
|
21
|
+
getTextElement(): HTMLDivElement | null
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* # Set Text
|
|
25
|
+
* @param text The text that will be visible inside the splash.
|
|
26
|
+
*/
|
|
27
|
+
setText(text: string): Splash
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* # Show Text
|
|
31
|
+
* Display text element.
|
|
32
|
+
*/
|
|
33
|
+
showText(): Splash
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* # Hide Text
|
|
37
|
+
* Hide the text element.
|
|
38
|
+
*/
|
|
39
|
+
hideText(): Splash
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* # Delete
|
|
43
|
+
* Remove from DOM and delete internal data.
|
|
44
|
+
*/
|
|
45
|
+
delete(): boolean
|
|
46
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { GUIDString } from '../types/Types'
|
|
2
|
+
import { Splash } from './Splash'
|
|
3
|
+
|
|
4
|
+
export class SplashQueue {
|
|
5
|
+
public queue: Array<Splash>
|
|
6
|
+
|
|
7
|
+
constructor() {
|
|
8
|
+
this.queue = []
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* # Enqueue
|
|
13
|
+
* Add a new item to the queue.
|
|
14
|
+
* @param item The item to add to the queue.
|
|
15
|
+
*/
|
|
16
|
+
public enqueue(item: Splash): SplashQueue {
|
|
17
|
+
this.queue.push(item)
|
|
18
|
+
return this
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* # Dequeue
|
|
23
|
+
* Remove the first item from the queue.
|
|
24
|
+
* @returns The first item from the queue.
|
|
25
|
+
*/
|
|
26
|
+
public dequeue(): Splash | undefined {
|
|
27
|
+
return this.queue.shift()
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* # Get
|
|
32
|
+
* @param id The GUID of the Nanosplash to get.
|
|
33
|
+
* @returns The Nanosplash with the given GUID.
|
|
34
|
+
*/
|
|
35
|
+
public get(id: GUIDString): Splash | undefined {
|
|
36
|
+
return this.queue.find(item => item.getId() === id)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* # Delete
|
|
41
|
+
* @param id The GUID of the Nanosplash to delete.
|
|
42
|
+
* @returns The deleted Nanosplash.
|
|
43
|
+
*/
|
|
44
|
+
public delete(id: GUIDString): Splash | undefined {
|
|
45
|
+
const index = this.queue.findIndex(item => item.getId() === id)
|
|
46
|
+
if (index === -1) return undefined
|
|
47
|
+
return this.queue.splice(index, 1)[0]
|
|
48
|
+
}
|
|
49
|
+
}
|
package/src/ts/main.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import NS from '../core/Service'
|
|
2
|
+
import { ServiceInterface } from '../core/ServiceInterface'
|
|
3
|
+
|
|
4
|
+
export class Service extends NS.Service {}
|
|
5
|
+
|
|
6
|
+
declare global {
|
|
7
|
+
export const ns: ServiceInterface
|
|
8
|
+
export interface Window {
|
|
9
|
+
ns: ServiceInterface
|
|
10
|
+
}
|
|
11
|
+
export interface window {
|
|
12
|
+
ns: ServiceInterface
|
|
13
|
+
}
|
|
14
|
+
}
|
package/src/vite-env.d.ts
CHANGED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// @ts-strict
|
|
2
|
+
|
|
3
|
+
import { JSDOM } from 'jsdom'
|
|
4
|
+
import { beforeEach, describe, expect, it } from 'vitest'
|
|
5
|
+
import {
|
|
6
|
+
createElement,
|
|
7
|
+
injectAsFirstChild,
|
|
8
|
+
showElement,
|
|
9
|
+
hideElement,
|
|
10
|
+
move,
|
|
11
|
+
elementFrom,
|
|
12
|
+
getRecycledSplash,
|
|
13
|
+
addNSHostClass,
|
|
14
|
+
removeNSHostClass,
|
|
15
|
+
} from '../src/ts/core/DomUtilities'
|
|
16
|
+
import { Splash } from '../src/ts/core/Splash'
|
|
17
|
+
import { Service } from '../src/ts/core/Service'
|
|
18
|
+
|
|
19
|
+
describe('DOMUtilities', () => {
|
|
20
|
+
// Reset the DOM before each test
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
const dom = new JSDOM()
|
|
23
|
+
// @ts-ignore
|
|
24
|
+
globalThis.window = dom.window
|
|
25
|
+
globalThis.document = dom.window.document
|
|
26
|
+
globalThis.DOMParser = dom.window.DOMParser
|
|
27
|
+
globalThis.Element = dom.window.Element
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('Should be able to create a Nanosplash component', () => {
|
|
31
|
+
const getClass = (element: Element) => element.classList[0]
|
|
32
|
+
const element = createElement()
|
|
33
|
+
const contentElement = <Element>element.firstElementChild
|
|
34
|
+
const textElement = <Element>contentElement.firstElementChild
|
|
35
|
+
const spinnerElement = <Element>textElement.nextElementSibling
|
|
36
|
+
|
|
37
|
+
expect(getClass(element)).toBe('ns')
|
|
38
|
+
expect(getClass(contentElement)).toBe('nsc')
|
|
39
|
+
expect(getClass(textElement)).toBe('nst')
|
|
40
|
+
expect(getClass(spinnerElement)).toBe('nss')
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('Should be able to inject a Nanosplash component', () => {
|
|
44
|
+
const elementA = createElement()
|
|
45
|
+
const elementB = createElement()
|
|
46
|
+
const destinationElement = document.createElement('div')
|
|
47
|
+
injectAsFirstChild(elementA, destinationElement)
|
|
48
|
+
expect(destinationElement.firstElementChild).toBe(elementA)
|
|
49
|
+
injectAsFirstChild(elementB, destinationElement)
|
|
50
|
+
expect(destinationElement.firstElementChild).toBe(elementB)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('Should be able to prepare the parent of a Nanosplash component', () => {
|
|
54
|
+
const ns = new Splash()
|
|
55
|
+
const parent = document.createElement('div')
|
|
56
|
+
parent.appendChild(ns.getElement() as Element)
|
|
57
|
+
addNSHostClass(parent)
|
|
58
|
+
expect(parent.classList.contains(Splash.NSHostClass)).toBe(true)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('Should be able to clean the parent of a Nanosplash component', () => {
|
|
62
|
+
const ns = new Splash()
|
|
63
|
+
const parent = document.createElement('div')
|
|
64
|
+
parent.appendChild(ns.getElement() as Element)
|
|
65
|
+
parent.classList.add(Splash.NSClass)
|
|
66
|
+
removeNSHostClass(parent)
|
|
67
|
+
expect(parent.classList.contains(Splash.NSHostClass)).toBe(false)
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('Should be able to convert an element reference to an element', () => {
|
|
71
|
+
const element = document.createElement('div')
|
|
72
|
+
document.body.appendChild(element)
|
|
73
|
+
element.id = 'test-id'
|
|
74
|
+
element.classList.add('test-class')
|
|
75
|
+
element.setAttribute('data-test', 'test')
|
|
76
|
+
expect(elementFrom('#test-id')).toBe(element)
|
|
77
|
+
expect(elementFrom('.test-class')).toBe(element)
|
|
78
|
+
expect(elementFrom('div[data-test="test"]')).toBe(element)
|
|
79
|
+
expect(elementFrom(element)).toBe(element)
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it('Should be able to recycle a Nanosplash component', () => {
|
|
83
|
+
const destination = document.createElement('div')
|
|
84
|
+
document.body.appendChild(destination)
|
|
85
|
+
const nss = Service.getInstance()
|
|
86
|
+
const id = nss.showInside(destination, 'Hello World!')
|
|
87
|
+
const recycled = getRecycledSplash(destination)
|
|
88
|
+
expect(recycled?.getId()).toBe(id)
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('Should be able to show an element', () => {
|
|
92
|
+
const element = document.createElement('div')
|
|
93
|
+
showElement(element)
|
|
94
|
+
expect(element.style.display).toBe('flex')
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('Should be able to hide an element', () => {
|
|
98
|
+
const element = document.createElement('div')
|
|
99
|
+
hideElement(element)
|
|
100
|
+
expect(element.style.display).toBe('none')
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
it('Should be able to alter the class of a Nanosplash host', () => {
|
|
104
|
+
const div = document.createElement('div')
|
|
105
|
+
addNSHostClass(div)
|
|
106
|
+
expect(div.classList.contains(Splash.NSHostClass)).toBe(true)
|
|
107
|
+
removeNSHostClass(div)
|
|
108
|
+
expect(div.classList.contains(Splash.NSHostClass)).toBe(false)
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
it('Should be able to move a Nanosplash component', () => {
|
|
112
|
+
const ns = new Splash()
|
|
113
|
+
const destination = document.createElement('div')
|
|
114
|
+
move(ns.getElement() as Element, destination)
|
|
115
|
+
expect(destination.firstElementChild).toBe(ns.getElement())
|
|
116
|
+
})
|
|
117
|
+
})
|
package/tests/Guid.spec.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { beforeEach, describe, expect, it } from 'vitest'
|
|
2
|
-
import Nanosplash from '../src/ts/core/Nanosplash/Nanosplash'
|
|
3
2
|
import { JSDOM } from 'jsdom'
|
|
3
|
+
import { Splash } from '../src/ts/core/Splash'
|
|
4
|
+
|
|
5
|
+
const rgx = /^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$/
|
|
4
6
|
|
|
5
7
|
describe('Guid', () => {
|
|
6
8
|
// Reset the DOM before each test
|
|
@@ -13,9 +15,7 @@ describe('Guid', () => {
|
|
|
13
15
|
})
|
|
14
16
|
|
|
15
17
|
it('Should generate a valid GUID', () => {
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
const value = new Nanosplash().getId()
|
|
19
|
-
expect(value).toMatch(guidRegex)
|
|
18
|
+
const value = new Splash().getId()
|
|
19
|
+
expect(value).toMatch(rgx)
|
|
20
20
|
})
|
|
21
21
|
})
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { beforeEach, expect, describe, it } from 'vitest'
|
|
2
|
+
import { Service } from '../src/ts/core/Service'
|
|
3
|
+
import { Splash } from '../src/ts/core/Splash'
|
|
4
|
+
import { GUIDString } from '../src/ts/types/Types'
|
|
5
|
+
import { ExpectedParsedValues, div, resetDOM } from './TestUtilities'
|
|
6
|
+
|
|
7
|
+
resetDOM()
|
|
8
|
+
|
|
9
|
+
declare global {
|
|
10
|
+
interface Window {
|
|
11
|
+
ns: Service
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
describe('NanosplashService', () => {
|
|
16
|
+
// Reset the DOM before each test
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
resetDOM()
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('Should be able to create an instance', () => {
|
|
22
|
+
const nss = Service.getInstance()
|
|
23
|
+
expect(nss).toBeInstanceOf(Service)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('Should be a valid singleton instance', () => {
|
|
27
|
+
const nss1 = Service.getInstance()
|
|
28
|
+
const nss2 = Service.getInstance()
|
|
29
|
+
expect(nss1).toBe(nss2)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('Should be able to start the service and attach it to Window', () => {
|
|
33
|
+
Service.start()
|
|
34
|
+
const nss = Service.getInstance()
|
|
35
|
+
expect(nss).toBe(window.ns)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('Should be able to show a Nanosplash in the browser window', () => {
|
|
39
|
+
const text = 'Hello World!'
|
|
40
|
+
const nss = Service.getInstance()
|
|
41
|
+
const nsId = <GUIDString>nss.show(text)
|
|
42
|
+
|
|
43
|
+
// Compare the elements:
|
|
44
|
+
const element = document.getElementById(nsId)
|
|
45
|
+
const splash = nss.nsQueue.get(nsId)
|
|
46
|
+
expect(element).toBe(splash?.getElement())
|
|
47
|
+
|
|
48
|
+
// Compare the text:
|
|
49
|
+
const nsText = splash?.getTextElement().innerText
|
|
50
|
+
expect(nsText).toBe(text)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('Should be able to show a Nanosplash over a given element', () => {
|
|
54
|
+
const id = 'test-id'
|
|
55
|
+
const text = 'Hello World!'
|
|
56
|
+
const destination = document.createElement('div')
|
|
57
|
+
|
|
58
|
+
// Assign ID and append to DOM
|
|
59
|
+
destination.id = id
|
|
60
|
+
document.body.appendChild(destination)
|
|
61
|
+
|
|
62
|
+
const nss = Service.getInstance()
|
|
63
|
+
|
|
64
|
+
// Show Splash inside element
|
|
65
|
+
const nsId = nss.showInside(`#${id}`, text)
|
|
66
|
+
|
|
67
|
+
// Get element from DOM with the same ID
|
|
68
|
+
const element = document.getElementById(<string>nsId)
|
|
69
|
+
|
|
70
|
+
// Get Splash instance from NS Service with the same ID
|
|
71
|
+
const ns = nss.nsQueue.get(<string>nsId)
|
|
72
|
+
|
|
73
|
+
// Compare the two
|
|
74
|
+
expect(element).toBe(ns?.getElement())
|
|
75
|
+
|
|
76
|
+
// Compare the text
|
|
77
|
+
const nsText = ns?.getTextElement().innerText
|
|
78
|
+
expect(nsText).toBe(text)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('Should be able to hide a Splash', () => {
|
|
82
|
+
const nss = Service.getInstance()
|
|
83
|
+
const nsId = <GUIDString>nss.show()
|
|
84
|
+
nss.hide()
|
|
85
|
+
const splash = nss.nsQueue.get(nsId)
|
|
86
|
+
expect(splash).toBeUndefined()
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('Should be able to hide multiple Splashes in FIFO-order', () => {
|
|
90
|
+
document.body.append(div('a'), div('b'), div('c'))
|
|
91
|
+
|
|
92
|
+
const nss = Service.getInstance()
|
|
93
|
+
const a = nss.showInside('#a', 'A')
|
|
94
|
+
const b = nss.showInside('#b', 'B')
|
|
95
|
+
const c = nss.showInside('#c', 'C')
|
|
96
|
+
|
|
97
|
+
expect(nss.nsQueue.queue.length).toBe(3)
|
|
98
|
+
|
|
99
|
+
nss.hide()
|
|
100
|
+
expect(nss.nsQueue.queue.length).toBe(2)
|
|
101
|
+
expect(nss.nsQueue.get(a as string)).toBeUndefined()
|
|
102
|
+
expect(nss.nsQueue.get(b as string)).toBeInstanceOf(Splash)
|
|
103
|
+
expect(nss.nsQueue.get(c as string)).toBeInstanceOf(Splash)
|
|
104
|
+
|
|
105
|
+
nss.hide()
|
|
106
|
+
expect(nss.nsQueue.queue.length).toBe(1)
|
|
107
|
+
expect(nss.nsQueue.get(a as string)).toBeUndefined()
|
|
108
|
+
expect(nss.nsQueue.get(b as string)).toBeUndefined()
|
|
109
|
+
expect(nss.nsQueue.get(c as string)).toBeInstanceOf(Splash)
|
|
110
|
+
|
|
111
|
+
nss.hide()
|
|
112
|
+
expect(nss.nsQueue.queue.length).toBe(0)
|
|
113
|
+
expect(nss.nsQueue.get(a as string)).toBeUndefined()
|
|
114
|
+
expect(nss.nsQueue.get(b as string)).toBeUndefined()
|
|
115
|
+
expect(nss.nsQueue.get(c as string)).toBeUndefined()
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it('Should be able to hide all Nanosplashes', () => {
|
|
119
|
+
document.body.append(div('a'), div('b'), div('c'))
|
|
120
|
+
|
|
121
|
+
const nss = Service.getInstance()
|
|
122
|
+
|
|
123
|
+
nss.hideAll() // Reset the queue
|
|
124
|
+
expect(nss.nsQueue.queue.length).toBe(0)
|
|
125
|
+
|
|
126
|
+
// Add three Nanosplashes to the queue
|
|
127
|
+
nss.showInside('#a', 'A')
|
|
128
|
+
nss.showInside('#b', 'B')
|
|
129
|
+
nss.showInside('#c', 'C')
|
|
130
|
+
|
|
131
|
+
// Expect the queue to have three items
|
|
132
|
+
expect(nss.nsQueue.queue.length).toBe(3)
|
|
133
|
+
|
|
134
|
+
// Hide all Nanosplashes
|
|
135
|
+
nss.hideAll()
|
|
136
|
+
|
|
137
|
+
// Expect the queue to be empty
|
|
138
|
+
expect(nss.nsQueue.queue.length).toBe(0)
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
it('Should be able to hide a Nanosplash based on its ID', () => {
|
|
142
|
+
const nss = Service.getInstance()
|
|
143
|
+
const nsId = <GUIDString>nss.show()
|
|
144
|
+
nss.hideId(nsId)
|
|
145
|
+
const splash = nss.nsQueue.get(nsId)
|
|
146
|
+
expect(splash).toBeUndefined()
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
it('Should be able to hide a Nanosplash residing inside a given element', () => {
|
|
150
|
+
const id = 'test-id'
|
|
151
|
+
const parent = document.createElement('div')
|
|
152
|
+
parent.id = id
|
|
153
|
+
document.body.appendChild(parent)
|
|
154
|
+
|
|
155
|
+
const nss = Service.getInstance()
|
|
156
|
+
const nsId = <GUIDString>nss.showInside(`#${id}`)
|
|
157
|
+
|
|
158
|
+
// Confirm existence.
|
|
159
|
+
const element = document.getElementById(<string>nsId)
|
|
160
|
+
expect(element?.parentElement).toBe(parent)
|
|
161
|
+
|
|
162
|
+
// Hide using API.
|
|
163
|
+
nss.hideInside(`#${id}`)
|
|
164
|
+
|
|
165
|
+
// Confirm non-existence.
|
|
166
|
+
expect(element?.isConnected).toBe(false)
|
|
167
|
+
expect(nss.nsQueue.get(nsId)).toBeUndefined()
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
it('Should take any argument as text without breaking', () => {
|
|
171
|
+
const nss = Service.getInstance()
|
|
172
|
+
|
|
173
|
+
// Destructure the expected values
|
|
174
|
+
const {
|
|
175
|
+
numValues,
|
|
176
|
+
arrValues,
|
|
177
|
+
objValues,
|
|
178
|
+
boolValues,
|
|
179
|
+
nullValues,
|
|
180
|
+
undefValues,
|
|
181
|
+
} = ExpectedParsedValues
|
|
182
|
+
|
|
183
|
+
// Create functions to test the values
|
|
184
|
+
const testValues = (raw: any, parsed: any) => {
|
|
185
|
+
const id = <GUIDString>nss.show(raw)
|
|
186
|
+
const value = nss.nsQueue.get(id)?.getTextElement().innerText
|
|
187
|
+
expect(value).toBe(parsed)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Test the values
|
|
191
|
+
testValues(numValues.raw, numValues.parsed)
|
|
192
|
+
testValues(arrValues.raw, arrValues.parsed)
|
|
193
|
+
testValues(objValues.raw, objValues.parsed)
|
|
194
|
+
testValues(boolValues.raw, boolValues.parsed)
|
|
195
|
+
testValues(nullValues.raw, nullValues.parsed)
|
|
196
|
+
testValues(undefValues.raw, undefValues.parsed)
|
|
197
|
+
})
|
|
198
|
+
})
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach } from 'vitest'
|
|
2
|
+
import { Splash } from '../src/ts/core/Splash'
|
|
3
|
+
import { resetDOM } from './TestUtilities'
|
|
4
|
+
|
|
5
|
+
resetDOM()
|
|
6
|
+
|
|
7
|
+
describe('Splash', () => {
|
|
8
|
+
// Reset the DOM before each test
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
resetDOM()
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it('Should be a valid Splash element', () => {
|
|
14
|
+
const ns = new Splash()
|
|
15
|
+
const element = ns.getElement() as HTMLElement
|
|
16
|
+
|
|
17
|
+
// The element should be a valid HTMLDivElement
|
|
18
|
+
expect(element).not.toBeUndefined()
|
|
19
|
+
expect(element).toBeInstanceOf(HTMLDivElement)
|
|
20
|
+
|
|
21
|
+
// The element and the splash instance should have the same ID
|
|
22
|
+
expect(element.id).toBe(ns.getId())
|
|
23
|
+
|
|
24
|
+
// The element should have the main Nanosplash CSS class
|
|
25
|
+
expect(element.classList.contains(Splash.NSClass)).toBe(true)
|
|
26
|
+
|
|
27
|
+
// The element should have the correct, inner structure
|
|
28
|
+
const textSelector = '.nsc > .nst'
|
|
29
|
+
const spinnerSelector = '.nsc > .nss'
|
|
30
|
+
const textElement = element.querySelector(textSelector)
|
|
31
|
+
const spinnerElement = element.querySelector(spinnerSelector)
|
|
32
|
+
expect(textElement).not.toBeNull()
|
|
33
|
+
expect(spinnerElement).not.toBeNull()
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('Should be able to set text', () => {
|
|
37
|
+
const ns = new Splash()
|
|
38
|
+
const text = 'Hello World!'
|
|
39
|
+
ns.setText(text)
|
|
40
|
+
expect(ns.getTextElement().innerText).toBe(text)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('Should be able to hide text', () => {
|
|
44
|
+
const ns = new Splash()
|
|
45
|
+
ns.hideText()
|
|
46
|
+
expect(ns.getTextElement().style.display).toBe('none')
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('Should be able to show text', () => {
|
|
50
|
+
const ns = new Splash()
|
|
51
|
+
ns.setText('Hello World!')
|
|
52
|
+
ns.hideText()
|
|
53
|
+
ns.showText()
|
|
54
|
+
expect(ns.getTextElement().style.display).toBe('flex')
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('Should be able to remove itself from DOM', () => {
|
|
58
|
+
const ns = new Splash()
|
|
59
|
+
const deleteResult = ns.delete()
|
|
60
|
+
const element = ns.getElement()
|
|
61
|
+
expect(deleteResult).toBe(true)
|
|
62
|
+
expect(element).toBeNull()
|
|
63
|
+
})
|
|
64
|
+
})
|