Skip to content

Commit

Permalink
test: add tests for nullishAttributeConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
motss committed Sep 17, 2021
1 parent e02e768 commit bf371e6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/helpers/nullish-attribute-converter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ComplexAttributeConverter } from 'lit';

export const nullishAttributeConverter: ComplexAttributeConverter<unknown>['toAttribute'] =
<T = string>(value: T) => value || undefined;
export const nullishAttributeConverter: NonNullable<ComplexAttributeConverter<unknown>['toAttribute']> =
<T = string>(value: T) => value || undefined;
28 changes: 28 additions & 0 deletions src/tests/helpers/nullish-attribute-converter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { expect } from '@open-wc/testing';

import { nullishAttributeConverter } from '../../helpers/nullish-attribute-converter';
import { messageFormatter } from '../test-utils/message-formatter';

type A = [unknown, string | undefined];

const cases: A[] = [
[null, undefined],
[undefined, undefined],
['', undefined],
['test', 'test'],
];

describe(nullishAttributeConverter.name, () => {
cases.forEach((a) => {
it(
messageFormatter('returns normalized attribute value (value=%s)', a),
() => {
const [testValue, expected] = a;

const result = nullishAttributeConverter(testValue);

expect(result).equal(expected);
}
);
});
});

0 comments on commit bf371e6

Please sign in to comment.