enigmatic 0.25.0 → 0.27.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/__tests__/e2.test.js +92 -0
- package/components.js +0 -111
- package/e2.js +10 -0
- package/index.html +1 -0
- package/jest.config.js +2 -1
- package/jest.setup.js +5 -0
- package/package.json +3 -2
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
const fs = require('fs')
|
|
2
|
+
const path = require('path')
|
|
3
|
+
|
|
4
|
+
// Load e2.js into jsdom
|
|
5
|
+
const e2Code = fs.readFileSync(path.join(__dirname, '../e2.js'), 'utf8')
|
|
6
|
+
|
|
7
|
+
describe('e2.js', () => {
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
// Reset DOM
|
|
10
|
+
global.document.body.innerHTML = ''
|
|
11
|
+
global.document.head.innerHTML = ''
|
|
12
|
+
|
|
13
|
+
// Clear window.custom
|
|
14
|
+
global.window.custom = {}
|
|
15
|
+
|
|
16
|
+
// Execute e2.js code
|
|
17
|
+
eval(e2Code)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
describe('fetchJson', () => {
|
|
21
|
+
test('fetches JSON with GET method', async () => {
|
|
22
|
+
const result = await window.fetchJson('GET', 'https://httpbin.org/json')
|
|
23
|
+
|
|
24
|
+
expect(result.status).toBe(200)
|
|
25
|
+
expect(result.statusText).toBe('OK')
|
|
26
|
+
expect(result.data).toBeDefined()
|
|
27
|
+
expect(typeof result.data).toBe('object')
|
|
28
|
+
expect(result.headers).toBeDefined()
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
test('fetches JSON with POST method and body', async () => {
|
|
32
|
+
const requestBody = { name: 'Test', value: 123 }
|
|
33
|
+
|
|
34
|
+
const result = await window.fetchJson('POST', 'https://httpbin.org/post', {
|
|
35
|
+
body: JSON.stringify(requestBody)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
expect(result.status).toBe(200)
|
|
39
|
+
expect(result.data).toBeDefined()
|
|
40
|
+
expect(result.data.json).toEqual(requestBody)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test('fetches JSON with PUT method', async () => {
|
|
44
|
+
const requestBody = { name: 'Updated' }
|
|
45
|
+
|
|
46
|
+
const result = await window.fetchJson('PUT', 'https://httpbin.org/put', {
|
|
47
|
+
body: JSON.stringify(requestBody)
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
expect(result.status).toBe(200)
|
|
51
|
+
expect(result.data).toBeDefined()
|
|
52
|
+
expect(result.data.json).toEqual(requestBody)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('fetches JSON with DELETE method', async () => {
|
|
56
|
+
const result = await window.fetchJson('DELETE', 'https://httpbin.org/delete')
|
|
57
|
+
|
|
58
|
+
expect(result.status).toBe(200)
|
|
59
|
+
expect(result.data).toBeDefined()
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
test('includes Content-Type header', async () => {
|
|
63
|
+
const result = await window.fetchJson('POST', 'https://httpbin.org/post', {
|
|
64
|
+
body: JSON.stringify({ test: 'data' })
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
expect(result.status).toBe(200)
|
|
68
|
+
expect(result.data.headers['Content-Type']).toBe('application/json')
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
test('includes credentials in request', async () => {
|
|
72
|
+
const result = await window.fetchJson('GET', 'https://httpbin.org/get')
|
|
73
|
+
|
|
74
|
+
expect(result.status).toBe(200)
|
|
75
|
+
// httpbin doesn't expose credentials, but we verify the request succeeded
|
|
76
|
+
expect(result.data).toBeDefined()
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
test('handles error responses', async () => {
|
|
80
|
+
// httpbin status endpoints return empty body, so fetchJson will throw on JSON parse
|
|
81
|
+
await expect(
|
|
82
|
+
window.fetchJson('GET', 'https://httpbin.org/status/404')
|
|
83
|
+
).rejects.toThrow()
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
test('handles network errors', async () => {
|
|
87
|
+
await expect(
|
|
88
|
+
window.fetchJson('GET', 'https://invalid-domain-that-does-not-exist-12345.com/api')
|
|
89
|
+
).rejects.toThrow()
|
|
90
|
+
})
|
|
91
|
+
})
|
|
92
|
+
})
|
package/components.js
CHANGED
|
@@ -5,117 +5,6 @@ window.components = {
|
|
|
5
5
|
await loadJS('https://cdn.jsdelivr.net/npm/marked/marked.min.js')
|
|
6
6
|
this.innerHTML = marked.parse(this.innerText)
|
|
7
7
|
}
|
|
8
|
-
},
|
|
9
|
-
"auto-complete": {
|
|
10
|
-
init() {
|
|
11
|
-
// Prevent re-initialization
|
|
12
|
-
if (this._initialized) return
|
|
13
|
-
this._initialized = true
|
|
14
|
-
|
|
15
|
-
// Ensure element is visible
|
|
16
|
-
this.style.display = 'block'
|
|
17
|
-
this.style.width = '100%'
|
|
18
|
-
this.style.minWidth = '200px'
|
|
19
|
-
this.style.margin = '10px 0'
|
|
20
|
-
this.style.padding = '0'
|
|
21
|
-
this.style.backgroundColor = 'transparent'
|
|
22
|
-
|
|
23
|
-
this.input = document.createElement('input')
|
|
24
|
-
this.input.type = 'text'
|
|
25
|
-
this.input.placeholder = this.getAttribute('placeholder') || 'Type to search...'
|
|
26
|
-
this.input.style.cssText = 'width:100%;padding:8px;border:1px solid #ccc;border-radius:4px;box-sizing:border-box;font-size:14px;'
|
|
27
|
-
|
|
28
|
-
this.dropdown = document.createElement('div')
|
|
29
|
-
this.dropdown.style.cssText = 'position:absolute;top:100%;left:0;right:0;background:white;border:1px solid #ccc;border-top:none;max-height:200px;overflow-y:auto;z-index:1000;display:none;box-shadow:0 2px 4px rgba(0,0,0,0.1);'
|
|
30
|
-
|
|
31
|
-
this.container = document.createElement('div')
|
|
32
|
-
this.container.style.cssText = 'position:relative;width:100%;min-width:200px;'
|
|
33
|
-
this.container.appendChild(this.input)
|
|
34
|
-
this.container.appendChild(this.dropdown)
|
|
35
|
-
|
|
36
|
-
// Clear existing content
|
|
37
|
-
this.innerHTML = ''
|
|
38
|
-
this.appendChild(this.container)
|
|
39
|
-
|
|
40
|
-
this.items = []
|
|
41
|
-
this.filtered = []
|
|
42
|
-
this.selectedIndex = -1
|
|
43
|
-
|
|
44
|
-
this.input.addEventListener('input', () => this.filter())
|
|
45
|
-
this.input.addEventListener('keydown', (e) => this.handleKey(e))
|
|
46
|
-
this.input.addEventListener('focus', () => {
|
|
47
|
-
if (this.filtered.length > 0) this.dropdown.style.display = 'block'
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
// Use a unique handler per instance
|
|
51
|
-
this._clickHandler = (e) => {
|
|
52
|
-
if (!this.contains(e.target)) {
|
|
53
|
-
this.dropdown.style.display = 'none'
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
document.addEventListener('click', this._clickHandler)
|
|
57
|
-
},
|
|
58
|
-
set(data) {
|
|
59
|
-
this.items = Array.isArray(data) ? data : (data?.items || [])
|
|
60
|
-
this.filter()
|
|
61
|
-
},
|
|
62
|
-
filter() {
|
|
63
|
-
const query = this.input.value.toLowerCase()
|
|
64
|
-
this.filtered = this.items.filter(item => {
|
|
65
|
-
const text = typeof item === 'string' ? item : (item.label || item.name || String(item))
|
|
66
|
-
return text.toLowerCase().includes(query)
|
|
67
|
-
})
|
|
68
|
-
this.render()
|
|
69
|
-
},
|
|
70
|
-
render() {
|
|
71
|
-
if (this.filtered.length === 0) {
|
|
72
|
-
this.dropdown.style.display = 'none'
|
|
73
|
-
return
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
this.dropdown.innerHTML = this.filtered.map((item, i) => {
|
|
77
|
-
const text = typeof item === 'string' ? item : (item.label || item.name || String(item))
|
|
78
|
-
const selected = i === this.selectedIndex ? 'background:#f0f0f0;' : ''
|
|
79
|
-
return `<div data-index="${i}" style="padding:8px;cursor:pointer;${selected}">${text}</div>`
|
|
80
|
-
}).join('')
|
|
81
|
-
|
|
82
|
-
this.dropdown.style.display = 'block'
|
|
83
|
-
|
|
84
|
-
this.dropdown.querySelectorAll('div').forEach((div, i) => {
|
|
85
|
-
div.addEventListener('click', () => this.select(i))
|
|
86
|
-
div.addEventListener('mouseenter', () => {
|
|
87
|
-
this.selectedIndex = i
|
|
88
|
-
this.render()
|
|
89
|
-
})
|
|
90
|
-
})
|
|
91
|
-
},
|
|
92
|
-
select(index) {
|
|
93
|
-
const item = this.filtered[index]
|
|
94
|
-
const text = typeof item === 'string' ? item : (item.label || item.name || String(item))
|
|
95
|
-
this.input.value = text
|
|
96
|
-
this.dropdown.style.display = 'none'
|
|
97
|
-
this.selectedIndex = -1
|
|
98
|
-
|
|
99
|
-
const event = new CustomEvent('select', { detail: item })
|
|
100
|
-
this.dispatchEvent(event)
|
|
101
|
-
},
|
|
102
|
-
handleKey(e) {
|
|
103
|
-
if (e.key === 'ArrowDown') {
|
|
104
|
-
e.preventDefault()
|
|
105
|
-
this.selectedIndex = Math.min(this.selectedIndex + 1, this.filtered.length - 1)
|
|
106
|
-
this.render()
|
|
107
|
-
} else if (e.key === 'ArrowUp') {
|
|
108
|
-
e.preventDefault()
|
|
109
|
-
this.selectedIndex = Math.max(this.selectedIndex - 1, -1)
|
|
110
|
-
this.render()
|
|
111
|
-
} else if (e.key === 'Enter' && this.selectedIndex >= 0) {
|
|
112
|
-
e.preventDefault()
|
|
113
|
-
this.select(this.selectedIndex)
|
|
114
|
-
} else if (e.key === 'Escape') {
|
|
115
|
-
this.dropdown.style.display = 'none'
|
|
116
|
-
this.selectedIndex = -1
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
8
|
}
|
|
120
9
|
}
|
|
121
10
|
|
package/e2.js
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
window.$ = document.querySelector.bind(document)
|
|
2
2
|
window.$$ = document.querySelectorAll.bind(document)
|
|
3
3
|
|
|
4
|
+
window.fetchJson = async (method, url, opts) => {
|
|
5
|
+
const res = await fetch(url, { method, ...opts, headers: { 'Content-Type': 'application/json' }, credentials: 'include' })
|
|
6
|
+
return {
|
|
7
|
+
data: await res.json(),
|
|
8
|
+
status: res.status,
|
|
9
|
+
statusText: res.statusText,
|
|
10
|
+
headers: res.headers
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
4
14
|
window.custom = {
|
|
5
15
|
"hello-world": (data) => `Hello ${data}`,
|
|
6
16
|
"hello-world-2": {
|
package/index.html
CHANGED
package/jest.config.js
CHANGED
package/jest.setup.js
CHANGED
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "enigmatic",
|
|
3
3
|
"main": "enigmatic.js",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.27.0",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "jest",
|
|
7
7
|
"test:watch": "jest --watch"
|
|
8
8
|
},
|
|
9
9
|
"devDependencies": {
|
|
10
10
|
"jest": "^29.7.0",
|
|
11
|
-
"jest-environment-jsdom": "^29.7.0"
|
|
11
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
12
|
+
"whatwg-fetch": "^3.6.20"
|
|
12
13
|
}
|
|
13
14
|
}
|