import '../../../dist/zn.min.js';
import {expect, fixture, html} from '@open-wc/testing';
import type ZnEditor from './editor.component';
describe('', () => {
it('should render a component', async () => {
const el = await fixture(html` `);
expect(el).to.exist;
});
describe('content caching', () => {
const KEY = 'zned:test-draft';
afterEach(() => {
sessionStorage.removeItem(KEY);
localStorage.removeItem(KEY);
});
it('should restore cached content into an empty editor', async () => {
sessionStorage.setItem(KEY, '0,Hello draft
');
const el = await fixture(html`
`);
expect(el.value).to.contain('Hello draft');
});
it('should not overwrite an initial value with cached content', async () => {
sessionStorage.setItem(KEY, '0,Hello draft
');
const el = await fixture(html`
Existing
'}>`);
expect(el.value).to.contain('Existing');
expect(el.value).to.not.contain('Hello draft');
});
it('should read from localStorage when local-storage is set', async () => {
localStorage.setItem(KEY, '0,Local draft
');
const el = await fixture(html`
`);
expect(el.value).to.contain('Local draft');
});
it('should cache content when the editor changes', async () => {
const el = await fixture(html`
Typed reply'}>`);
document.dispatchEvent(new Event('zn-editor-update'));
await el.updateComplete;
// Quill's getSemanticHTML encodes spaces as
expect(sessionStorage.getItem(KEY)).to.contain('Typed reply');
});
it('should clear cached content on form submit', async () => {
const form = await fixture(html`
`);
const el = form.querySelector('zn-editor')!;
document.dispatchEvent(new Event('zn-editor-update'));
await el.updateComplete;
expect(sessionStorage.getItem(KEY)).to.contain('Typed reply');
form.requestSubmit();
expect(sessionStorage.getItem(KEY)).to.equal(null);
});
});
});