brut-js 0.0.2 → 0.0.5

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/package.json CHANGED
@@ -1,13 +1,16 @@
1
1
  {
2
2
  "name": "brut-js",
3
- "version": "0.0.2",
3
+ "version": "0.0.5",
4
4
  "type": "module",
5
5
  "keywords": [ "WebComponents", "Custom Elements" ],
6
6
  "bugs": {
7
7
  "url": "https://github.com/thirdtank/brut-js/issues",
8
8
  "email": "davec@naildrivin5.com"
9
9
  },
10
- "main": "src/index.js",
10
+ "exports": {
11
+ ".": "./src/index.js",
12
+ "./testing": "./src/testing/index.js"
13
+ },
11
14
  "repository": "https://github.com/thirdtank/brut-js",
12
15
  "author": {
13
16
  "name": "David Copeland",
@@ -1,4 +1,4 @@
1
- import { withHTML, readRequestBodyIntoString, waitForSetTimeout } from "../src/testing/index.js"
1
+ import { withHTML } from "./SpecHelper.js"
2
2
 
3
3
  describe("<brut-ajax-submit>", () => {
4
4
  withHTML(`
@@ -12,7 +12,9 @@ describe("<brut-ajax-submit>", () => {
12
12
  `).onFetch( "/foo", [
13
13
  { then: { status: 200 }},
14
14
  ]
15
- ).test("submits the form, setting various attributes during the lifecycle", ({document,assert,fetchRequests}) => {
15
+ ).test("submits the form, setting various attributes during the lifecycle",
16
+ ({document,assert,fetchRequests,waitForSetTimeout,readRequestBodyIntoString}) => {
17
+
16
18
  const element = document.querySelector("brut-ajax-submit")
17
19
  const button = element.querySelector("button")
18
20
  const text = document.querySelector("input[name=some-text]")
@@ -80,7 +82,8 @@ describe("<brut-ajax-submit>", () => {
80
82
  { then: { status: 500 }},
81
83
  { then: { status: 200 }},
82
84
  ]
83
- ).test("submits the form after a retry", ({document,assert,fetchRequests}) => {
85
+ ).test("submits the form after a retry",
86
+ ({document,assert,fetchRequests,waitForSetTimeout,readRequestBodyIntoString}) => {
84
87
  const element = document.querySelector("brut-ajax-submit")
85
88
  const button = element.querySelector("button")
86
89
  const text = document.querySelector("input[name=some-text]")
@@ -124,7 +127,7 @@ describe("<brut-ajax-submit>", () => {
124
127
  { then: { status: 500 }},
125
128
  { then: { status: 500 }},
126
129
  ]
127
- ).test("when too many failures, submits the form the old-fashioned way", ({document,assert,fetchRequests}) => {
130
+ ).test("when too many failures, submits the form the old-fashioned way", ({document,assert,fetchRequests, waitForSetTimeout}) => {
128
131
  const form = document.querySelector("form")
129
132
  const element = form.querySelector("brut-ajax-submit")
130
133
  const button = element.querySelector("button")
@@ -195,7 +198,7 @@ Error that should be ignored
195
198
  }
196
199
  },
197
200
  ]
198
- ).test("when we get a 422, parses the result from the server", ({document,window,assert,fetchRequests}) => {
201
+ ).test("when we get a 422, parses the result from the server", ({document,window,assert,fetchRequests,waitForSetTimeout}) => {
199
202
  const form = document.querySelector("form")
200
203
  const element = form.querySelector("brut-ajax-submit")
201
204
  const button = element.querySelector("button")
@@ -1,4 +1,4 @@
1
- import { withHTML } from "../src/testing/index.js"
1
+ import { withHTML } from "./SpecHelper.js"
2
2
 
3
3
  describe("<brut-autosubmit>", () => {
4
4
  withHTML(`
@@ -1,4 +1,4 @@
1
- import { withHTML } from "../src/testing/index.js"
1
+ import { withHTML } from "./SpecHelper.js"
2
2
 
3
3
  describe("<brut-confirm-submit>", () => {
4
4
  describe("without an explicit dialog", () => {
@@ -1,4 +1,4 @@
1
- import { withHTML } from "../src/testing/index.js"
1
+ import { withHTML } from "./SpecHelper.js"
2
2
 
3
3
  describe("<brut-constraint-violation-message>", () => {
4
4
  withHTML(`
@@ -1,4 +1,4 @@
1
- import { withHTML } from "../src/testing/index.js"
1
+ import { withHTML } from "./SpecHelper.js"
2
2
 
3
3
  describe("<brut-constraint-violation-messages>", () => {
4
4
  withHTML(`
@@ -1,4 +1,4 @@
1
- import { withHTML } from "../src/testing/index.js"
1
+ import { withHTML } from "./SpecHelper.js"
2
2
 
3
3
  describe("<brut-form>", () => {
4
4
  withHTML(`
@@ -133,4 +133,48 @@ describe("<brut-form>", () => {
133
133
  assert(gotValid)
134
134
  assert(!gotInvalid)
135
135
  })
136
+ withHTML(`
137
+ <brut-form>
138
+ <form>
139
+ <label>
140
+ <input required type="text" name="text">
141
+ </label>
142
+ <brut-constraint-violation-messages input-name='text'>
143
+ </brut-constraint-violation-messages>
144
+ <input type="submit">Save</input>
145
+ </form>
146
+ </brut-form>
147
+ `).test("locates the messages for errors based on name", ({window,document,assert}) => {
148
+
149
+ const brutForm = document.querySelector("brut-form")
150
+ const form = brutForm.querySelector("form")
151
+ const button = form.querySelector("input[type=submit]")
152
+ const textFieldLabel = form.querySelector("label:has(input[type=text])")
153
+
154
+ let submitted = false
155
+ let gotInvalid = false
156
+ let gotValid = false
157
+
158
+ form.addEventListener("submit", (event) => {
159
+ event.preventDefault()
160
+ submitted = true
161
+ })
162
+ brutForm.addEventListener("brut:valid", () => {
163
+ gotValid = true
164
+ })
165
+ brutForm.addEventListener("brut:invalid", () => {
166
+ gotInvalid = true
167
+ })
168
+
169
+ button.click()
170
+
171
+ assert(!submitted)
172
+ assert(!gotValid)
173
+ assert(gotInvalid)
174
+ assert(form.dataset["submitted"] != null)
175
+
176
+ let error = brutForm.querySelector("brut-constraint-violation-message[input-name='text'][key='general.cv.fe.valueMissing']")
177
+ assert(error)
178
+
179
+ })
136
180
  })
@@ -1,4 +1,4 @@
1
- import { withHTML } from "../src/testing/index.js"
1
+ import { withHTML } from "./SpecHelper.js"
2
2
 
3
3
  describe("<brut-i18n-translation>", () => {
4
4
  withHTML(`
@@ -1,4 +1,4 @@
1
- import { withHTML, readRequestBodyIntoJSON } from "../src/testing/index.js"
1
+ import { withHTML } from "./SpecHelper.js"
2
2
 
3
3
  describe("<brut-locale-detection>", () => {
4
4
  withHTML(`
@@ -6,7 +6,7 @@ describe("<brut-locale-detection>", () => {
6
6
  `).onFetch( "/locale", [
7
7
  { then: { status: 200 }},
8
8
  ]
9
- ).test("Receives the locale and timeZone from the browser", ({document,assert,fetchRequests}) => {
9
+ ).test("Receives the locale and timeZone from the browser", ({document,assert,fetchRequests,readRequestBodyIntoJSON}) => {
10
10
 
11
11
  assert.equal(1,fetchRequests.length)
12
12
  return readRequestBodyIntoJSON(fetchRequests[0]).then( (json) => {
@@ -1,4 +1,4 @@
1
- import { withHTML } from "../src/testing/index.js"
1
+ import { withHTML } from "./SpecHelper.js"
2
2
 
3
3
  describe("<brut-message>", () => {
4
4
  withHTML(`
@@ -0,0 +1,23 @@
1
+ import { createTestBasedOnHTML } from "../src/testing/index.js"
2
+
3
+ import path from "node:path"
4
+ import fs from "node:fs"
5
+
6
+ const __dirname = import.meta.dirname
7
+
8
+ const appRoot = path.resolve(__dirname)
9
+ const publicRoot = path.resolve(appRoot,"public")
10
+ const assetMetadataFilePath = path.resolve(appRoot,"config","asset_metadata.json")
11
+ const assetMetadata = JSON.parse(fs.readFileSync(assetMetadataFilePath))
12
+
13
+ const withHTML = (html) => {
14
+ return createTestBasedOnHTML({
15
+ html,
16
+ assetMetadata,
17
+ publicRoot
18
+ })
19
+ }
20
+
21
+ export {
22
+ withHTML,
23
+ }
@@ -1,4 +1,4 @@
1
- import { withHTML } from "../src/testing/index.js"
1
+ import { withHTML } from "./SpecHelper.js"
2
2
 
3
3
  describe("<brut-tabs>", () => {
4
4
  withHTML(`
@@ -978,9 +978,24 @@
978
978
  #updateErrorMessages(event) {
979
979
  const element = event.target;
980
980
  const selector = `${ConstraintViolationMessages_default.tagName}:not([server-side])`;
981
- const errorLabels = element.parentNode.querySelectorAll(selector);
981
+ let errorLabels = element.parentNode.querySelectorAll(selector);
982
+ if (errorLabels.length == 0) {
983
+ if (element.name && element.form) {
984
+ const moreGeneralSelector = `${ConstraintViolationMessages_default.tagName}[input-name='${element.name}']:not([server-side])`;
985
+ errorLabels = element.form.querySelectorAll(moreGeneralSelector);
986
+ if (errorLabels.length == 0) {
987
+ this.logger.warn(`Did not find any elements matching ${selector} or ${moreGeneralSelector}, so no error messages will be shown`);
988
+ }
989
+ } else {
990
+ this.logger.warn(
991
+ "Did not find any elements matching %s and the form element has %s %s",
992
+ selector,
993
+ element.name ? "no name" : "a name, but",
994
+ element.form ? "no form" : "though has a form"
995
+ );
996
+ }
997
+ }
982
998
  if (errorLabels.length == 0) {
983
- this.logger.warn(`Did not find any elements matching ${selector}, so no error messages will be shown`);
984
999
  return;
985
1000
  }
986
1001
  let anyErrors = false;