Skip to content

Commit

Permalink
Merge pull request #55 from Gavant/bug/fix-types-for-validator
Browse files Browse the repository at this point in the history
Fix types for validator and add tests
  • Loading branch information
bakerac4 authored Jan 10, 2023
2 parents 55e18a6 + 931219e commit d624111
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 23 deletions.
1 change: 0 additions & 1 deletion floating-labels/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"@glint/core": "^0.9.2",
"@glint/environment-ember-loose": "^0.9.2",
"@rollup/plugin-babel": "^5.3.1",
"@types/ember-qunit": "^6.1.1",
"@types/ember-resolver": "^5.0.11",
"@types/ember__application": "^4.0.0",
"@types/ember__array": "^4.0.1",
Expand Down
20 changes: 14 additions & 6 deletions floating-labels/src/components/fl-input/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface FlInputArgs<V> {
id?: string;
name?: string;
type?: HTMLInputType;
errors?: string[] | string;
errors?: string | string[] | (string | string[])[];
}

interface BaseFloatingSignature<V> {
Expand Down Expand Up @@ -92,11 +92,19 @@ export abstract class BaseFloatingLabelClass<
}

get firstError(): string | null {
return Array.isArray(this.args.errors)
? this.args.errors[0]
: typeof this.args.errors === 'string'
? this.args.errors
: null;
const isArray = Array.isArray(this.args.errors);

if (this.args.errors && isArray) {
if (Array.isArray(this.args.errors?.[0])) {
return this.args.errors[0][0];
} else {
return this.args.errors[0];
}
} else if (typeof this.args.errors === 'string') {
return this.args.errors;
} else {
return null;
}
}

get placeholder(): string | undefined {
Expand Down
3 changes: 1 addition & 2 deletions test-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"devDependencies": {
"@ember/optional-features": "^2.0.0",
"@ember/render-modifiers": "^2.0.4",
"@ember/test-helpers": "^2.9.1",
"@ember/test-helpers": "^2.9.3",
"@embroider/compat": "^1.8.3",
"@embroider/core": "^1.8.3",
"@embroider/test-setup": "^1.7.1",
Expand All @@ -39,7 +39,6 @@
"@glimmer/tracking": "^1.1.2",
"@glint/core": "^0.9.2",
"@glint/environment-ember-loose": "^0.9.2",
"@types/ember-qunit": "^6.1.1",
"@types/ember-resolver": "^5.0.11",
"@types/ember__application": "^4.0.0",
"@types/ember__array": "^4.0.1",
Expand Down
25 changes: 22 additions & 3 deletions test-app/tests/integration/components/fl-input-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,32 @@ module('Integration | Component | fl-input', function (hooks) {
test('it renders', async function (assert) {
await render(hbs`<FlInput @placeholder="test"/>`);
await a11yAudit();
assert.strictEqual(this.element.textContent?.trim(), 'test');
assert.dom().hasText('test');
});

test('Type attribute is set on the <input> when using <FlInput />', async function (assert) {
await render(hbs`<FlInput @placeholder="password" @type="password"/>`);
await a11yAudit();
const inputElement = this.element.querySelector('input') as HTMLInputElement;
assert.strictEqual(inputElement.type, 'password');
assert.dom('input').hasAttribute('type', 'password');
});

test('Single error works', async function (assert) {
await render(hbs`<FlInput @placeholder="password" @type="password" @errors="test"/>`);
await a11yAudit();
assert.dom().hasText('test');
});

test('Array error works', async function (assert) {
this.set('errors', ['test', 'test2']);
await render(hbs`<FlInput @placeholder="password" @type="password" @errors={{this.errors}}/>`);
await a11yAudit();
assert.dom().hasText('test');
});

test('Array of Arrays error works', async function (assert) {
this.set('errors', [['test', 'test2']]);
await render(hbs`<FlInput @placeholder="password" @type="password" @errors={{this.errors}}/>`);
await a11yAudit();
assert.dom().hasText('test');
});
});
2 changes: 1 addition & 1 deletion test-app/tests/integration/components/fl-select-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ module('Integration | Component | fl-select', function (hooks) {

await render(hbs`<FlSelect @placeholder="test" />`);
await a11yAudit();
assert.strictEqual(this.element.textContent?.trim(), 'test');
assert.dom().hasText('test');
});
});
2 changes: 1 addition & 1 deletion test-app/tests/integration/components/fl-textarea-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ module('Integration | Component | fl-textarea', function (hooks) {

await render(hbs`<FlTextarea @placeholder="test" />`);
await a11yAudit();
assert.strictEqual(this.element.textContent?.trim(), 'test');
assert.dom().hasText('test');
});
});
11 changes: 2 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@
dependencies:
ember-cli-babel "^7.26.6"

"@ember/test-helpers@^2.9.1":
"@ember/test-helpers@^2.9.3":
version "2.9.3"
resolved "https://registry.yarnpkg.com/@ember/test-helpers/-/test-helpers-2.9.3.tgz#c2a9d6ab1c367af92cf1a334f97eb19b8e06e6e1"
integrity sha512-ejVg4Dj+G/6zyLvQsYOvmGiOLU6AS94tY4ClaO1E2oVvjjtVJIRmVLFN61I+DuyBg9hS3cFoPjQRTZB9MRIbxQ==
Expand Down Expand Up @@ -2104,13 +2104,6 @@
resolved "https://registry.yarnpkg.com/@types/cors/-/cors-2.8.12.tgz#6b2c510a7ad7039e98e7b8d3d6598f4359e5c080"
integrity sha512-vt+kDhq/M2ayberEtJcIN/hxXy1Pk+59g2FV/ZQceeaTyCtCucjL2Q7FXlFjtWn4n15KCr1NE2lNNFhp0lEThw==

"@types/ember-qunit@^6.1.1":
version "6.1.1"
resolved "https://registry.yarnpkg.com/@types/ember-qunit/-/ember-qunit-6.1.1.tgz#8350944aced173024135d9ae2b7e55105c44564e"
integrity sha512-1g5A3vPKhvB/CuN/EP9rBLXYaJOjzKyLYWeBtNDEQNkTuG1dAo/Hg0CCixgbBgLlzaDP8mR/n1xpg8HqQ8SUKg==
dependencies:
ember-qunit "*"

"@types/ember-resolver@*", "@types/ember-resolver@^5.0.11":
version "5.0.11"
resolved "https://registry.yarnpkg.com/@types/ember-resolver/-/ember-resolver-5.0.11.tgz#db931fb5c2d6bda4e29adea132fb48c7ed17aa62"
Expand Down Expand Up @@ -6267,7 +6260,7 @@ ember-page-title@^7.0.0:
dependencies:
ember-cli-babel "^7.26.6"

ember-qunit@*, ember-qunit@^6.1.1:
ember-qunit@^6.1.1:
version "6.1.1"
resolved "https://registry.yarnpkg.com/ember-qunit/-/ember-qunit-6.1.1.tgz#9e2b7835239e7ffd176e08e536975f66e59f75e5"
integrity sha512-3/jCpoecltFV6vm7GCtSDNRxBzWx96nP+QrbereJAnioSaGeLe+slKL3l80mGzMYDTvn7ESobZ+Ba+OQ1vMMKQ==
Expand Down

0 comments on commit d624111

Please sign in to comment.