@natachah/vanilla-frontend 0.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.
Files changed (104) hide show
  1. package/.gitlab-ci.yml +40 -0
  2. package/LICENSE.md +7 -0
  3. package/README.md +11 -0
  4. package/docs/index.html +36 -0
  5. package/docs/main.js +32 -0
  6. package/docs/pages/components/badge.html +154 -0
  7. package/docs/pages/components/button.html +186 -0
  8. package/docs/pages/components/card.html +184 -0
  9. package/docs/pages/components/dialog.html +334 -0
  10. package/docs/pages/components/disclosure.html +310 -0
  11. package/docs/pages/components/dropdown.html +255 -0
  12. package/docs/pages/components/form.html +331 -0
  13. package/docs/pages/components/list.html +140 -0
  14. package/docs/pages/components/loading.html +58 -0
  15. package/docs/pages/components/media.html +130 -0
  16. package/docs/pages/components/nav.html +119 -0
  17. package/docs/pages/components/progress.html +47 -0
  18. package/docs/pages/components/slider.html +311 -0
  19. package/docs/pages/components/table.html +168 -0
  20. package/docs/pages/javascript/autofill.html +170 -0
  21. package/docs/pages/javascript/checkall.html +59 -0
  22. package/docs/pages/javascript/comfort.html +134 -0
  23. package/docs/pages/javascript/consent.html +112 -0
  24. package/docs/pages/javascript/cookie.html +81 -0
  25. package/docs/pages/javascript/form.html +199 -0
  26. package/docs/pages/javascript/scroll.html +209 -0
  27. package/docs/pages/javascript/sidebar.html +53 -0
  28. package/docs/pages/javascript/sortable.html +148 -0
  29. package/docs/pages/javascript/toggle.html +191 -0
  30. package/docs/pages/javascript/tree.html +221 -0
  31. package/docs/pages/layout/grid.html +201 -0
  32. package/docs/pages/layout/reset.html +53 -0
  33. package/docs/pages/layout/typography.html +324 -0
  34. package/docs/pages/quick-start/conventions.html +112 -0
  35. package/docs/pages/quick-start/customization.html +187 -0
  36. package/docs/pages/quick-start/installation.html +95 -0
  37. package/docs/pages/quick-start/mixins.html +228 -0
  38. package/docs/pages/test.html +15 -0
  39. package/docs/src/js/demo.js +98 -0
  40. package/docs/src/js/doc-code.js +102 -0
  41. package/docs/src/js/doc-demo.js +14 -0
  42. package/docs/src/js/doc-layout.js +108 -0
  43. package/docs/src/scss/demo.scss +77 -0
  44. package/docs/src/scss/layout.scss +160 -0
  45. package/docs/src/scss/style.scss +278 -0
  46. package/docs/vite.config.mjs +23 -0
  47. package/esbuild.mjs +25 -0
  48. package/js/_autofill.js +131 -0
  49. package/js/_check-all.js +77 -0
  50. package/js/_comfort.js +174 -0
  51. package/js/_consent.js +84 -0
  52. package/js/_dialog.js +164 -0
  53. package/js/_dropdown.js +101 -0
  54. package/js/_scroll.js +184 -0
  55. package/js/_sidebar.js +97 -0
  56. package/js/_slider.js +249 -0
  57. package/js/_sortable.js +143 -0
  58. package/js/_tabpanel.js +88 -0
  59. package/js/_toggle.js +123 -0
  60. package/js/_tree.js +85 -0
  61. package/js/tests/autofill.test.js +157 -0
  62. package/js/tests/base-component.test.js +108 -0
  63. package/js/tests/check-all.test.js +88 -0
  64. package/js/tests/comfort.test.js +219 -0
  65. package/js/tests/consent.test.js +84 -0
  66. package/js/tests/cookie.test.js +102 -0
  67. package/js/tests/dialog.test.js +189 -0
  68. package/js/tests/dropdown.test.js +115 -0
  69. package/js/tests/form-helper.test.js +155 -0
  70. package/js/tests/scroll.test.js +203 -0
  71. package/js/tests/sidebar.test.js +99 -0
  72. package/js/tests/slider.test.js +307 -0
  73. package/js/tests/sortable.test.js +124 -0
  74. package/js/tests/tabpanel.test.js +114 -0
  75. package/js/tests/toggle.test.js +190 -0
  76. package/js/tests/tree.test.js +165 -0
  77. package/js/utilities/_base-component.js +101 -0
  78. package/js/utilities/_cookie.js +98 -0
  79. package/js/utilities/_error.js +80 -0
  80. package/js/utilities/_form-helper.js +101 -0
  81. package/package.json +42 -0
  82. package/scss/_badge.scss +37 -0
  83. package/scss/_button.scss +34 -0
  84. package/scss/_card.scss +122 -0
  85. package/scss/_dialog.scss +116 -0
  86. package/scss/_disclosure.scss +101 -0
  87. package/scss/_dropdown.scss +68 -0
  88. package/scss/_form.scss +197 -0
  89. package/scss/_grid.scss +40 -0
  90. package/scss/_group.scss +57 -0
  91. package/scss/_list.scss +18 -0
  92. package/scss/_loading.scss +49 -0
  93. package/scss/_media.scss +37 -0
  94. package/scss/_nav.scss +72 -0
  95. package/scss/_progress.scss +40 -0
  96. package/scss/_slider.scss +35 -0
  97. package/scss/_table.scss +36 -0
  98. package/scss/utilities/_mixin.scss +322 -0
  99. package/scss/utilities/_reset.scss +145 -0
  100. package/scss/utilities/_typography.scss +107 -0
  101. package/scss/vanilla-frontend.scss +23 -0
  102. package/scss/variables/_root.scss +70 -0
  103. package/scss/variables/_setting.scss +63 -0
  104. package/vitest.config.js +7 -0
@@ -0,0 +1,108 @@
1
+ /**
2
+ * ------------------------------------------------------------------
3
+ * TEST for the utilities/_base-component.js
4
+ * ------------------------------------------------------------------
5
+ * The test will take care of:
6
+ * - Has all the public method
7
+ * - Constructor: Passing the correct parameters
8
+ * - Constructor: Return the correct properties
9
+ * - MergeObject(): Passing the correct parameter
10
+ * - MergeObject(): Return the correct value
11
+ * - MergeObject(): Multilevel object
12
+ * - EmmitEvent(): Passing the correct parameter
13
+ * - EmmitEvent(): Return the correct value
14
+ * - EmmitEvent(): Event listener
15
+ * - EmmitEvent(): Prevent default
16
+ */
17
+
18
+ import { describe, test, expect, beforeAll } from "vitest"
19
+ import { fireEvent } from "@testing-library/dom"
20
+ import BaseComponent from "../utilities/_base-component"
21
+ import ErrorMessage from "../utilities/_error"
22
+
23
+ let fakeDiv, fakeComponent, fakeEvent
24
+
25
+ /**
26
+ * Before all tests
27
+ *
28
+ */
29
+
30
+ beforeAll(() => {
31
+ fakeDiv = document.createElement('div')
32
+ fakeComponent = new BaseComponent(fakeDiv, {})
33
+ fakeEvent = fakeComponent.emmitEvent('testing', { test: true }, fakeDiv)
34
+ })
35
+
36
+ describe('Structure of the class', () => {
37
+
38
+ test('Has all the public method', () => {
39
+ expect(fakeComponent.emmitEvent).toBeTypeOf('function')
40
+ })
41
+
42
+ test('Constructor: Passing the correct parameters', () => {
43
+ expect(() => new BaseComponent()).toThrowError(ErrorMessage.instanceOf('el', 'HTMLElement'))
44
+ expect(() => new BaseComponent('make-error')).toThrowError(ErrorMessage.instanceOf('el', 'HTMLElement'))
45
+ expect(() => new BaseComponent(fakeDiv, 'make-error')).toThrowError(ErrorMessage.instanceOf('options', 'Object'))
46
+ })
47
+
48
+ test('Constructor: Return the correct properties', () => {
49
+ expect(fakeComponent).toHaveProperty('_element')
50
+ expect(fakeComponent).toHaveProperty('_options')
51
+ expect(fakeComponent._element).toBe(fakeDiv)
52
+ expect(fakeComponent._options).toStrictEqual({})
53
+ })
54
+
55
+ })
56
+
57
+ describe('MergeObject()', () => {
58
+
59
+ test('Passing the correct parameter', () => {
60
+ expect(() => fakeComponent.mergeObject('make-error', {})).toThrowError(ErrorMessage.instanceOf('one', 'Object'))
61
+ expect(() => fakeComponent.mergeObject({}, 'make-error')).toThrowError(ErrorMessage.instanceOf('two', 'Object'))
62
+ })
63
+
64
+ test('Return the correct value', () => {
65
+ const a = { one: 1, two: 2, tree: 3 }
66
+ const b = { tree: 'three', four: 4 }
67
+ const result = fakeComponent.mergeObject(a, b)
68
+ expect(result).toStrictEqual({ one: 1, two: 2, tree: 'three' })
69
+ })
70
+
71
+ test('Multilevel object', () => {
72
+ const a = { level: 1, sublevel: { one: 1, two: 2 } }
73
+ const b = { level: 'one', sublevel: { one: 'one' } }
74
+ const result = fakeComponent.mergeObject(a, b)
75
+ expect(result).toStrictEqual({ level: 'one', sublevel: { one: 'one', two: 2 } })
76
+ })
77
+
78
+ })
79
+
80
+ describe('EmmitEvent()', () => {
81
+
82
+ test('Passing the correct parameter', () => {
83
+ expect(() => fakeComponent.emmitEvent()).toThrowError(ErrorMessage.typeOf('name', 'string'))
84
+ expect(() => fakeComponent.emmitEvent('testing', 'make-error')).toThrowError(ErrorMessage.instanceOf('data', 'Object'))
85
+ expect(() => fakeComponent.emmitEvent('testing', {}, 'make-error')).toThrowError(ErrorMessage.instanceOf('selector', 'HTMLElement'))
86
+ })
87
+
88
+ test('Return the correct value', () => {
89
+ expect(fakeEvent).toBeInstanceOf(CustomEvent)
90
+ expect(fakeEvent.type).toBe('basecomponent:testing')
91
+ expect(fakeEvent.detail).toStrictEqual({ test: true })
92
+ })
93
+
94
+ test('Event listener', () => {
95
+ let steps = 0
96
+ fakeDiv.addEventListener('basecomponent:testing', () => steps++)
97
+ const myEvent = fireEvent(fakeDiv, fakeEvent)
98
+ expect(myEvent).toBe(true)
99
+ expect(steps).toBe(1)
100
+ })
101
+
102
+ test('Prevent default', () => {
103
+ fakeDiv.addEventListener('basecomponent:testing', (e) => e.preventDefault())
104
+ const myEvent = fireEvent(fakeDiv, fakeEvent)
105
+ expect(myEvent).toBe(false)
106
+ })
107
+
108
+ })
@@ -0,0 +1,88 @@
1
+ /**
2
+ * ------------------------------------------------------------------
3
+ * TEST for the _check-all.js
4
+ * ------------------------------------------------------------------
5
+ * The test will take care of:
6
+ * ! herit from BaseComponent - Constructor: Passing the correct parameters
7
+ * - Constructor: Return the correct properties
8
+ * - #Init(): Toggle checkboxes on change the _element
9
+ * - #Init(): Check all the checkboxes will set the :checked ont the _element
10
+ * - #Init(): Uncheck one of the checkboxes will remove the :checked on the _element
11
+ */
12
+
13
+ import { describe, test, expect, beforeAll } from "vitest"
14
+ import { fireEvent } from "@testing-library/dom"
15
+ import CheckAll from "../_check-all"
16
+
17
+ let fakeCheckbox
18
+
19
+ /**
20
+ * Before all tests
21
+ *
22
+ */
23
+
24
+ beforeAll(() => {
25
+
26
+ document.body.innerHTML =
27
+ '<input id="fakeCheckbox" type="checkbox" name="all" value="fake[]">' +
28
+ '<input id="one" type="checkbox" name="fake[]" value="1" checked>' +
29
+ '<input id="two" type="checkbox" name="fake[]" value="2">' +
30
+ '<input id="three" type="checkbox" name="fake[]" value="3">'
31
+
32
+ fakeCheckbox = new CheckAll(document.getElementById('fakeCheckbox'))
33
+
34
+ })
35
+
36
+ describe('Structure of the class', () => {
37
+
38
+ test('Return the correct properties', () => {
39
+ expect(fakeCheckbox._checkboxes).toBeTypeOf('object')
40
+ expect(fakeCheckbox._checkboxes).toStrictEqual(document.querySelectorAll('input[type="checkbox"][name="fake[]"]'))
41
+ expect(fakeCheckbox.number).toBeInstanceOf(Object)
42
+ expect(fakeCheckbox.number).toStrictEqual({
43
+ total: 3,
44
+ checked: 1,
45
+ unchecked: 2
46
+ })
47
+ })
48
+
49
+ })
50
+
51
+ describe('#Init()', () => {
52
+
53
+ test('Toggle checkboxes on change the _element', () => {
54
+ expect(fakeCheckbox._element.checked).toBeFalsy()
55
+ fireEvent.change(fakeCheckbox._element, { target: { checked: true } })
56
+ expect(fakeCheckbox._element.checked).toBeTruthy()
57
+ expect(fakeCheckbox.number.checked).toBe(3)
58
+ expect(fakeCheckbox.number.unchecked).toBe(0)
59
+ fireEvent.change(fakeCheckbox._element, { target: { checked: false } })
60
+ expect(fakeCheckbox._element.checked).toBeFalsy()
61
+ expect(fakeCheckbox.number.checked).toBe(0)
62
+ expect(fakeCheckbox.number.unchecked).toBe(3)
63
+ })
64
+
65
+ test('Check all the checkboxes will set the :checked ont the _element', () => {
66
+ const one = document.getElementById('one')
67
+ const two = document.getElementById('two')
68
+ const three = document.getElementById('three')
69
+ expect(fakeCheckbox._element.checked).toBeFalsy()
70
+ fireEvent.change(one, { target: { checked: true } })
71
+ expect(fakeCheckbox._element.checked).toBeFalsy()
72
+ expect(fakeCheckbox.number.checked).toBe(1)
73
+ fireEvent.change(two, { target: { checked: true } })
74
+ expect(fakeCheckbox._element.checked).toBeFalsy()
75
+ expect(fakeCheckbox.number.checked).toBe(2)
76
+ fireEvent.change(three, { target: { checked: true } })
77
+ expect(fakeCheckbox._element.checked).toBeTruthy()
78
+ expect(fakeCheckbox.number.checked).toBe(3)
79
+ })
80
+
81
+ test('Uncheck one of the checkboxes will remove the :checked on the _element', () => {
82
+ const one = document.getElementById('one')
83
+ fireEvent.change(one, { target: { checked: false } })
84
+ expect(fakeCheckbox._element.checked).toBeFalsy()
85
+ expect(fakeCheckbox.number.checked).toBe(2)
86
+ })
87
+
88
+ })
@@ -0,0 +1,219 @@
1
+ /**
2
+ * ------------------------------------------------------------------
3
+ * TEST for the _comfort.js
4
+ * ------------------------------------------------------------------
5
+ * The test will take care of:
6
+ * - Has all the public method
7
+ * ! herit from Cookie - Constructor: Passing the correct parameters
8
+ * - Constructor: Return the correct properties
9
+ * - #Init: Emit the setTheme() method when click on this._buttons.theme
10
+ * - #Init: Emit the setStyle() method when click on this._buttons.style
11
+ * - SetTheme(): Passing the correct parameters
12
+ * - SetTheme(): Save the property in the cookie
13
+ * - SetTheme(): Emmit the set() method
14
+ * - SetTheme(): If already exist, DONT emmit the set() method
15
+ * - SetStyle(): Passing the correct parameters
16
+ * - SetStyle(): Save the property in the cookie
17
+ * - SetStyle(): Emmit the set() and #toggleStyle() method
18
+ * - SetStyle(): If already exist, DONT emmit the set() and #toggleStyle() method
19
+ * - Reset(): Emmit de delete() and remove the cookie
20
+ * - Reset(): Remove all the attributes
21
+ * - #toggleTheme(): Add the [data-theme] attribute on the <html>
22
+ * - #toggleTheme(): Add the [aria-pressed] attribute on the <button>
23
+ * - #toggleStyle(): Add the [style] attribute on the <body>
24
+ * - #toggleStyle(): Add the [aria-pressed] attribute on the <button>
25
+ */
26
+
27
+ import { describe, test, expect, beforeAll, afterEach, afterAll, vi } from "vitest"
28
+ import { fireEvent } from "@testing-library/dom"
29
+ import Comfort from "../_comfort"
30
+ import ErrorMessage from "../utilities/_error"
31
+
32
+ let fakeComfort
33
+
34
+ /**
35
+ * Before all tests
36
+ *
37
+ */
38
+
39
+ beforeAll(() => {
40
+
41
+ document.body.innerHTML =
42
+ '<button id="fakeDarkButton" data-theme="theme" value="dark"></button>' +
43
+ '<button id="fakeLightButton" data-theme="theme" value="light"></button>' +
44
+ '<button id="fakeFontButton" data-style="font-size" value="1rem"></button>'
45
+
46
+ fakeComfort = new Comfort()
47
+
48
+ })
49
+
50
+ describe('Structure of the class', () => {
51
+
52
+ test('Has all the public method', () => {
53
+ expect(fakeComfort.setTheme).toBeTypeOf('function')
54
+ expect(fakeComfort.setStyle).toBeTypeOf('function')
55
+ expect(fakeComfort.reset).toBeTypeOf('function')
56
+ })
57
+
58
+ test('Constructor: Return the correct properties', () => {
59
+ expect(fakeComfort._buttons).toBeTypeOf('object')
60
+ expect(fakeComfort._buttons.theme).toStrictEqual(document.querySelectorAll('[data-theme]'))
61
+ expect(fakeComfort._buttons.style).toStrictEqual(document.querySelectorAll('[data-style]'))
62
+ })
63
+
64
+ })
65
+
66
+ describe('#Init()', () => {
67
+
68
+ test('Emit the setTheme() method when click on this._buttons.theme', () => {
69
+ const methodSpy = vi.spyOn(fakeComfort, 'setTheme')
70
+ fireEvent(document.getElementById('fakeDarkButton'), new MouseEvent('click'))
71
+ expect(methodSpy).toHaveBeenCalled()
72
+ })
73
+
74
+ test('Emit the setStyle() method when click on this._buttons.style', () => {
75
+ const methodSpy = vi.spyOn(fakeComfort, 'setStyle')
76
+ fireEvent(document.getElementById('fakeFontButton'), new MouseEvent('click'))
77
+ expect(methodSpy).toHaveBeenCalled()
78
+ })
79
+
80
+ afterAll(() => {
81
+ fakeComfort.reset()
82
+ })
83
+
84
+ })
85
+
86
+ describe('SetTheme()', () => {
87
+
88
+ test('Passing the correct parameters', () => {
89
+ expect(() => fakeComfort.setTheme()).toThrowError(ErrorMessage.typeOf('value', 'string'))
90
+ })
91
+
92
+ test('Save the property in the cookie', () => {
93
+ expect(fakeComfort.has('theme')).toBeFalsy()
94
+ fakeComfort.setTheme('my-theme')
95
+ expect(fakeComfort.has('theme')).toBeTruthy()
96
+ expect(fakeComfort.get('theme')).toBe('my-theme')
97
+ })
98
+
99
+ test('Emmit the set() method', () => {
100
+ const methodSpy = vi.spyOn(fakeComfort, 'set')
101
+ fakeComfort.setTheme('dark')
102
+ expect(methodSpy).toHaveBeenCalled()
103
+ })
104
+
105
+ test('If already exist, DONT emmit the set() method', () => {
106
+ const methodSpy = vi.spyOn(fakeComfort, 'set')
107
+ fakeComfort.setTheme('dark')
108
+ expect(methodSpy).not.toHaveBeenCalled()
109
+ })
110
+
111
+ afterAll(() => {
112
+ fakeComfort.reset()
113
+ })
114
+
115
+ })
116
+
117
+ describe('SetStyle()', () => {
118
+
119
+ test('Passing the correct parameters', () => {
120
+ expect(() => fakeComfort.setStyle()).toThrowError(ErrorMessage.typeOf('name', 'string'))
121
+ expect(() => fakeComfort.setStyle('font-size')).toThrowError(ErrorMessage.typeOf('value', 'string'))
122
+ })
123
+
124
+ test('Save the property in the cookie', () => {
125
+ expect(fakeComfort.has('style')).toBeFalsy()
126
+ fakeComfort.setStyle('color', 'red')
127
+ expect(fakeComfort.has('style')).toBeTruthy()
128
+ expect(fakeComfort.get('style')).toStrictEqual({ 'color': 'red' })
129
+ })
130
+
131
+ test('Emmit the set() method', () => {
132
+ const methodSpy = vi.spyOn(fakeComfort, 'set')
133
+ fakeComfort.setStyle('font-size', '1rem')
134
+ expect(methodSpy).toHaveBeenCalled()
135
+ })
136
+
137
+ test('If already exist, DONT emmit the set() method', () => {
138
+ const methodSpy = vi.spyOn(fakeComfort, 'set')
139
+ fakeComfort.setStyle('font-size', '1rem')
140
+ expect(methodSpy).not.toHaveBeenCalled()
141
+ })
142
+
143
+ afterAll(() => {
144
+ fakeComfort.reset()
145
+ })
146
+
147
+ })
148
+
149
+ describe('Reset()', () => {
150
+
151
+ test('Emmit de delete() and remove the cookie', () => {
152
+ const methodSpy = vi.spyOn(fakeComfort, 'delete')
153
+ fakeComfort.setTheme('dark')
154
+ expect(fakeComfort.has('theme')).toBeTruthy()
155
+ fakeComfort.reset()
156
+ expect(fakeComfort.has('theme')).toBeFalsy()
157
+ expect(methodSpy).toHaveBeenCalled()
158
+ })
159
+
160
+ test('Remove all the attributes', () => {
161
+ const html = document.documentElement
162
+ const body = document.body
163
+ const buttonTheme = document.getElementById('fakeDarkButton')
164
+ const buttonStyle = document.getElementById('fakeFontButton')
165
+ fakeComfort.setTheme('dark')
166
+ fakeComfort.setStyle('font-size', '2rem')
167
+ expect(html.hasAttribute('data-theme')).toBeTruthy()
168
+ expect(body.hasAttribute('style')).toBeTruthy()
169
+ expect(buttonTheme.hasAttribute('aria-pressed')).toBeTruthy()
170
+ expect(buttonStyle.hasAttribute('aria-pressed')).toBeTruthy()
171
+ fakeComfort.reset()
172
+ expect(html.hasAttribute('data-theme')).toBeFalsy()
173
+ expect(body.hasAttribute('style')).toBeFalsy()
174
+ expect(buttonTheme.hasAttribute('aria-pressed')).toBeFalsy()
175
+ expect(buttonStyle.hasAttribute('aria-pressed')).toBeFalsy()
176
+ })
177
+
178
+ })
179
+
180
+
181
+ describe('#ToggleTheme()', () => {
182
+
183
+ afterEach(() => {
184
+ fakeComfort.reset()
185
+ })
186
+
187
+ test('Add the [data-theme] attribute on the <html>', () => {
188
+ const html = document.documentElement
189
+ expect(html.hasAttribute('data-theme')).toBeFalsy()
190
+ fakeComfort.setTheme('dark')
191
+ expect(html.hasAttribute('data-theme')).toBeTruthy()
192
+ expect(html.getAttribute('data-theme')).toBe('dark')
193
+ })
194
+
195
+ test('Add the [aria-pressed] attribute on the <button>', () => {
196
+ const button = document.getElementById('fakeDarkButton')
197
+ expect(button.hasAttribute('aria-pressed')).toBeFalsy()
198
+ fakeComfort.setTheme('dark')
199
+ expect(button.hasAttribute('aria-pressed')).toBeTruthy()
200
+ expect(button.getAttribute('aria-pressed')).toBe('true')
201
+ })
202
+
203
+ test('Add the [style] attribute on the <body>', () => {
204
+ const body = document.body
205
+ expect(body.hasAttribute('style')).toBeFalsy()
206
+ fakeComfort.setStyle('font-size', '1rem')
207
+ expect(body.hasAttribute('style')).toBeTruthy()
208
+ expect(body.getAttribute('style')).toBe('font-size:1rem')
209
+ })
210
+
211
+ test('Add the [aria-pressed] attribute on the <button>', () => {
212
+ const button = document.getElementById('fakeFontButton')
213
+ expect(button.hasAttribute('aria-pressed')).toBeFalsy()
214
+ fakeComfort.setStyle('font-size', '1rem')
215
+ expect(button.hasAttribute('aria-pressed')).toBeTruthy()
216
+ expect(button.getAttribute('aria-pressed')).toBe('true')
217
+ })
218
+
219
+ })
@@ -0,0 +1,84 @@
1
+ /**
2
+ * ------------------------------------------------------------------
3
+ * TEST for the _consent.js
4
+ * ------------------------------------------------------------------
5
+ * The test will take care of:
6
+ * ! herit from Cookie - Has all the public method
7
+ * - Constructor: Passing the correct parameters
8
+ * - Constructor: Return the correct properties
9
+ * - #Init(): Make sure that the dialog is open if the cookie doesn't exist
10
+ * - #Init(): Emmit the dialog:closed event will run the set() method
11
+ * - #Init(): Emmit the dialog:submiting event will run the set() method and save the inputs value
12
+ */
13
+
14
+ import { describe, test, expect, beforeAll, vi } from "vitest"
15
+ import Dialog from "../_dialog"
16
+ import Consent from "../_consent"
17
+ import ErrorMessage from "../utilities/_error"
18
+
19
+ let fakeCookie, fakeCookiePref, fakeDefaultCookie
20
+
21
+ /**
22
+ * Before all tests
23
+ *
24
+ */
25
+
26
+ beforeAll(() => {
27
+
28
+ document.body.innerHTML =
29
+ '<dialog id="cookies"></dialog>' +
30
+ '<dialog id="cookiesPreferences" aria-modal="true">' +
31
+ '<form method="dialog">' +
32
+ '<input type="checkbox" name="cookies_consent[]" value="necessary" checked disabled>' +
33
+ '<input type="checkbox" name="cookies_consent[]" value="analytic" checked>' +
34
+ '<input type="checkbox" name="cookies_consent[]" value="marketing" >' +
35
+ '</form>' +
36
+ '</dialog>'
37
+
38
+ fakeCookie = new Consent('_consent', 'cookies')
39
+ fakeCookiePref = new Consent('_consent', 'cookiesPreferences')
40
+ fakeDefaultCookie = new Consent()
41
+
42
+ })
43
+
44
+ describe('Structure of the class', () => {
45
+
46
+ test('Constructor: Passing the correct parameters', () => {
47
+ expect(() => new Consent('test', {})).toThrowError(ErrorMessage.typeOf('dialogID', 'string'))
48
+ expect(() => new Consent('test', 'make-error')).toThrowError(ErrorMessage.existById('dialog', 'make-error'))
49
+ })
50
+
51
+ test('Constructor: Return the correct properties', () => {
52
+ expect(fakeCookie).toHaveProperty('_dialog')
53
+ expect(fakeCookie._dialog).toBeInstanceOf(Dialog)
54
+ expect(fakeCookie._dialog._element).toStrictEqual(document.getElementById('cookies'))
55
+ expect(fakeCookie.value).toStrictEqual({})
56
+ expect(fakeDefaultCookie._dialog).toBeInstanceOf(Dialog)
57
+ })
58
+
59
+ })
60
+
61
+ describe('#Init()', () => {
62
+
63
+ test('Make sure that the dialog is open if the cookie doesn\'t exist', () => {
64
+ expect(fakeCookie.value).toStrictEqual({})
65
+ expect(fakeCookie._dialog.open).toBeTruthy()
66
+ })
67
+
68
+ test('Emmit the dialog:closed event will run the set() method and save the inputs value', () => {
69
+ const methodSpy = vi.spyOn(fakeCookie, 'set')
70
+ expect(methodSpy).not.toHaveBeenCalled()
71
+ fakeCookie._dialog.close()
72
+ expect(methodSpy).toHaveBeenCalled()
73
+ expect(fakeCookie.value).toStrictEqual({ necessary: true })
74
+ })
75
+
76
+ test('Emmit the dialog:submiting event will run the set() method and save the inputs value', () => {
77
+ const methodSpy = vi.spyOn(fakeCookiePref, 'set')
78
+ expect(methodSpy).not.toHaveBeenCalled()
79
+ fakeCookiePref._dialog.submit()
80
+ expect(methodSpy).toHaveBeenCalled()
81
+ expect(fakeCookie.value).toStrictEqual({ necessary: true, analytic: true, marketing: false })
82
+ })
83
+
84
+ })
@@ -0,0 +1,102 @@
1
+ /**
2
+ * ------------------------------------------------------------------
3
+ * TEST for the /utilities/_cookie.js
4
+ * ------------------------------------------------------------------
5
+ * The test will take care of:
6
+ * - Has all the public method
7
+ * - Constructor: Passing the correct parameters
8
+ * - Constructor: Return the correct properties
9
+ * - Set(): Passing the correct parameters
10
+ * - Set(): Save the new cookie value
11
+ * - Has(): Passing the correct parameters
12
+ * - Has(): Verify the existance of a key in the cookie value
13
+ * - Get(): Passing the correct parameters
14
+ * - Get(): Get value by key in the cookie
15
+ * - Delete(): Delete the cookie
16
+ */
17
+
18
+ import { describe, test, expect, beforeAll } from "vitest"
19
+ import Cookie from "../utilities/_cookie"
20
+ import ErrorMessage from "../utilities/_error"
21
+
22
+ let fakeCookie
23
+
24
+ /**
25
+ * Before all tests
26
+ *
27
+ */
28
+
29
+ beforeAll(() => {
30
+ fakeCookie = new Cookie('monster')
31
+ })
32
+
33
+ describe('Structure of the class', () => {
34
+
35
+ test('Has all the public method', () => {
36
+ expect(fakeCookie.set).toBeTypeOf('function')
37
+ expect(fakeCookie.has).toBeTypeOf('function')
38
+ expect(fakeCookie.get).toBeTypeOf('function')
39
+ expect(fakeCookie.delete).toBeTypeOf('function')
40
+ })
41
+
42
+ test('Constructor: Passing the correct parameters', () => {
43
+ expect(() => new Cookie()).toThrowError(ErrorMessage.typeOf('name', 'string'))
44
+ })
45
+
46
+ test('Constructor: Return the correct properties', () => {
47
+ expect(fakeCookie.value).toBeInstanceOf(Object)
48
+ expect(fakeCookie.value).toStrictEqual({})
49
+ })
50
+
51
+ })
52
+
53
+ describe('Set()', () => {
54
+
55
+ test('Passing the correct parameters ', () => {
56
+ expect(() => fakeCookie.set('make-error')).toThrowError(ErrorMessage.instanceOf('value', 'Object'))
57
+ })
58
+
59
+ test('Save the new cookie value', () => {
60
+ fakeCookie.set({ 1: 'one' })
61
+ expect(fakeCookie.value).toStrictEqual({ 1: 'one' })
62
+ fakeCookie.set({ 1: 'one', 2: 'two' })
63
+ expect(fakeCookie.value).toStrictEqual({ 1: 'one', 2: 'two' })
64
+ })
65
+
66
+ })
67
+
68
+ describe('Has()', () => {
69
+
70
+ test('Passing the correct parameters ', () => {
71
+ expect(() => fakeCookie.has()).toThrowError(ErrorMessage.typeOf('key', 'string'))
72
+ })
73
+
74
+ test('Verify the existance of a key in the cookie value', () => {
75
+ expect(fakeCookie.has('dont-exist')).toBeFalsy()
76
+ expect(fakeCookie.has('1')).toBeTruthy()
77
+ })
78
+
79
+ })
80
+
81
+ describe('Get()', () => {
82
+
83
+ test('Passing the correct parameters ', () => {
84
+ expect(() => fakeCookie.has()).toThrowError(ErrorMessage.typeOf('key', 'string'))
85
+ })
86
+
87
+ test('Get value by key in the cookie', () => {
88
+ expect(fakeCookie.get('dont-exist')).toBeNull()
89
+ expect(fakeCookie.get('1')).toBe('one')
90
+ })
91
+
92
+ })
93
+
94
+ describe('Delete()', () => {
95
+
96
+ test('Delete the cookie', () => {
97
+ expect(fakeCookie.value).toStrictEqual({ 1: 'one', 2: 'two' })
98
+ fakeCookie.delete()
99
+ expect(fakeCookie.value).toStrictEqual({})
100
+ })
101
+
102
+ })