Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

test(esl-mixin): additional prototype tests #1790

Merged
merged 5 commits into from
Jul 17, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions src/modules/esl-mixin-element/test/mixin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,108 @@ describe('ESLMixinElement', () => {
while (document.body.lastElementChild) document.body.removeChild(document.body.lastElementChild);
});
});

describe('ESLMixinElement prototype', () => {
class CTestMixin extends ESLMixinElement {
static override is = 'c-test';
}

const $host = document.createElement('div');
$host.toggleAttribute(CTestMixin.is, true);
CTestMixin.register();

beforeAll(() => document.body.appendChild($host));

describe('ESLMixinElement $attr', () => {
let $el: CTestMixin;
const attrName = 'test-attr';

beforeAll(() => $el = CTestMixin.get($host) as CTestMixin);

test('should return the attribute value', () => {
const val = $host.getAttribute(attrName);
expect($el.$$attr(attrName)).toBe(val);
});

test('should set the attribute value and return the new value', () => {
expect($el.$$attr(attrName, 'test')).toBe(null);
expect($el.$$attr(attrName)).toBe('test');
});
});

describe('ESLMixinElement $attr - boolean', () => {
let el: CTestMixin;
const attrName = 'test-attr-bool';

beforeAll(() => el = CTestMixin.get($host) as CTestMixin);

test('should return the initial attribute value', () => {
const val = $host.getAttribute(attrName);
expect(el.$$attr(attrName)).toBe(val);
});

test('should set and return an empty string for boolean true', () => {
expect(el.$$attr(attrName, true)).toBe(null);
expect(el.$$attr(attrName)).toBe('');
});

test('should set and return null for removed attribute', () => {
expect(el.$$attr(attrName, false)).toBe('');
expect(el.$$attr(attrName)).toBe(null);
});
});

describe('ESLMixinElement $cls - get', () => {
let $el: CTestMixin;

beforeAll(() => $el = CTestMixin.get($host) as CTestMixin);

test('should return false when class does not exist', () => expect($el.$$cls('a')).toBe(false));

test('should return true when class exists', () => {
$host.className = 'a b';
expect($el.$$cls('a')).toBe(true);
expect($el.$$cls('a b')).toBe(true);
});
});

describe('ESLMixinElement $cls - set', () => {
let $el: CTestMixin;

beforeAll(() => $el = CTestMixin.get($host) as CTestMixin);

test('should add the class when setting to true', () => {
$host.className = '';
$el.$$cls('a', true);
$el.$$cls('b', true);
expect($host.className).toBe('a b');
});

test('should remove the class when setting to false', () => {
$host.className = 'a b';
expect($el.$$cls('a b', false)).toBe(false);
expect($host.className).toBe('');
});
});

test('ESLMixinElement $$fire', (done) => {
const $el = CTestMixin.get($host) as CTestMixin;

$host.addEventListener('testevent', (e) => {
expect(e).toBeInstanceOf(CustomEvent);
done();
}, {once: true});
$el.$$fire('testevent');
}, 10);

test('ESLMixinElement $$fire - bubbling', (done) => {
const $el = CTestMixin.get($host) as CTestMixin;

document.addEventListener('testevent', (e) => {
expect(e).toBeInstanceOf(CustomEvent);
done();
}, {once: true});
$el.$$fire('testevent');
}, 10);
});
});