Skip to content

Commit a8775d6

Browse files
committed
combine unit test
1 parent eae3885 commit a8775d6

File tree

3 files changed

+67
-148
lines changed

3 files changed

+67
-148
lines changed

packages/usa-input-mask/src/test/input-mask-alphanumeric.spec.js

+55-15
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@ const TEMPLATE = fs.readFileSync(
1010
const EVENTS = {};
1111

1212
/**
13-
* send an input event
13+
* send an keydown event
1414
* @param {HTMLElement} el the element to sent the event to
1515
*/
16-
EVENTS.input = (el) => {
17-
el.dispatchEvent(new KeyboardEvent("keyup", { bubbles: true }));
16+
EVENTS.keydown = (el) => {
17+
el.dispatchEvent(new KeyboardEvent("keydown", { bubbles: true }));
1818
};
1919

20-
const inputMaskingSelector = () => document.querySelector(".usa-input-masking");
20+
const inputMaskSelector = () => document.querySelector(".usa-input-mask");
21+
// const inputMaskShellSelector = () =>
22+
// document.querySelector(".usa-input-mask__content");
23+
2124
const tests = [
2225
{ name: "document.body", selector: () => document.body },
23-
{ name: "input mask", selector: inputMaskingSelector },
26+
{ name: "input mask", selector: inputMaskSelector },
2427
];
2528

2629
tests.forEach(({ name, selector: containerSelector }) => {
@@ -29,27 +32,64 @@ tests.forEach(({ name, selector: containerSelector }) => {
2932

3033
let root;
3134
let input;
35+
let statusMessageVisual;
36+
let statusMessageSR;
3237
let shell;
3338

3439
beforeEach(() => {
3540
body.innerHTML = TEMPLATE;
36-
InputMask.on(containerSelector());
37-
38-
root = inputMaskingSelector();
39-
input = root.querySelector(".usa-input");
41+
root = containerSelector().parentNode;
42+
console.log('ROOT: ', root.outerHTML);
43+
InputMask.on(root);
44+
input = root.querySelector(".usa-input-mask");
45+
statusMessageVisual = root.querySelector("#alphanumericError");
46+
statusMessageSR = root.querySelector("#alphanumericErrorSrOnly");
4047
});
4148

4249
afterEach(() => {
43-
InputMask.off(containerSelector());
50+
InputMask.off(root);
4451
body.textContent = "";
4552
});
4653

47-
it("formats an alphanumeric example to A1B 2C3", () => {
48-
input.value = "A1B2C3";
54+
it("creates a visual status message on init", () => {
55+
const visibleStatus = document.getElementById("#alphanumericError");
56+
57+
assert.strictEqual(visibleStatus.length, 1);
58+
});
59+
60+
it("creates a screen reader status message on init", () => {
61+
const srStatus = document.querySelectorAll("#alphanumericErrorSrOnly");
62+
63+
assert.strictEqual(srStatus.length, 1);
64+
});
65+
66+
it("informs the user only a number character is allowed", () => {
67+
input.value = "a";
68+
69+
EVENTS.keydown(input);
4970

50-
EVENTS.input(input);
51-
shell = root.querySelector(".usa-input-mask--content");
52-
assert.strictEqual(shell.textContent, "A1B 2C3");
71+
assert.strictEqual(
72+
statusMessageVisual.innerHTML,
73+
"You must enter a number",
74+
);
5375
});
76+
77+
// it("formats an alphanumeric example to A1B 2C3", () => {
78+
// const value = "A1B2C3";
79+
80+
// for (let i = 0; i < value.length; i += 1) {
81+
// input.dispatchEvent(
82+
// new KeyboardEvent("keydown", {
83+
// bubbles: true,
84+
// key: value[i],
85+
// keyCode: value[i].charCodeAt(0),
86+
// which: value[i].charCodeAt(0),
87+
// })
88+
// );
89+
// }
90+
91+
// shell = inputMaskShellSelector();
92+
// assert.strictEqual(shell.textContent, "1EG4-TE5-MK73");
93+
// });
5494
});
5595
});
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
<div class="usa-input-masking">
2-
<h3 class="site-preview-heading">Alphanumeric Input Mask</h3>
3-
<label class="usa-label" for="zip">Alphanumeric example</label>
4-
<div class="usa-hint" id="alphanumericHint">For example, A1B 2C3</div>
5-
<span class="usa-input-mask" data-mask="___ ___">
6-
<span class="usa-input-mask--content test" aria-hidden="true" id="alphanumericMask"
7-
>___ ___</span
8-
>
1+
<form class="usa-form">
2+
<div class="usa-form-group">
3+
<label class="usa-label" for="alphanumeric">Alphanumeric</label>
4+
<div class="usa-hint" id="alphanumericHint">
5+
For example, A1B 2C3
6+
</div>
97
<input
8+
class="usa-input usa-input-mask"
9+
forceupper="true"
1010
id="alphanumeric"
11-
type="text"
1211
inputmode="text"
1312
name="alphanumeric"
14-
pattern="\w\d\w \d\w\d"
15-
class="usa-input usa-masked"
13+
type="text"
1614
aria-describedby="alphanumericHint"
1715
maxlength="7"
1816
data-placeholder="___ ___"
1917
data-charset="A#A #A#"
18+
placeholder="___ __ ____"
2019
/>
21-
</span>
22-
</div>
20+
</div>
21+
</form>

packages/usa-input-mask/src/test/input-mask-error-message.spec.js

-120
This file was deleted.

0 commit comments

Comments
 (0)