-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoscd-menu-save.spec.ts
More file actions
94 lines (81 loc) · 2.93 KB
/
oscd-menu-save.spec.ts
File metadata and controls
94 lines (81 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { expect, fixture, html } from '@open-wc/testing';
import { spy } from 'sinon';
import OscdMenuSave from './oscd-menu-save.js';
import sinon from 'sinon';
customElements.define('oscd-menu-save', OscdMenuSave);
const sclString = `<SCL version="2007" revision="B" xmlns="http://www.iec.ch/61850/2003/SCL" xmlns:ens1="http://example.org/somePreexistingExtensionNamespace">
<Substation ens1:foo="a" name="A1" desc="test substation"></Substation>
</SCL>`;
const sclXmlDocString = `<?xml version="1.0" encoding="UTF-8"?>${sclString}`;
const sclDoc = new DOMParser().parseFromString(
sclXmlDocString,
'application/xml',
);
describe('oscd-menu-save', () => {
let clickSpy = spy();
let plugin: OscdMenuSave;
let anchorElement: HTMLAnchorElement | null = null;
const originalCreateElement = document.createElement;
beforeEach(async () => {
document.createElement = function (tagName: string) {
const element = originalCreateElement.call(document, tagName);
if (tagName === 'a') {
anchorElement = element as HTMLAnchorElement;
element.click = clickSpy;
}
return element;
};
clickSpy = spy();
plugin = await fixture(html`<oscd-menu-save></oscd-menu-save>`);
plugin.docs = {
'test.scd': sclDoc,
};
plugin.doc = sclDoc;
plugin.docName = 'test.scd';
});
afterEach(() => {
plugin.remove();
anchorElement = null;
document.createElement = originalCreateElement;
});
it('opens the file save as dialog', async () => {
sinon.stub(window, 'setTimeout').callsFake(fn => {
fn();
return 0 as unknown as ReturnType<typeof setTimeout>;
});
await plugin.run();
sinon.restore();
expect(clickSpy.callCount).to.equal(1);
if (anchorElement) {
const a = anchorElement as HTMLAnchorElement;
expect(a.download).to.equal('test.scd');
expect(a.href).to.include('blob:');
expect(a.dataset.downloadurl).to.include('application/xml');
} else {
throw new Error('Anchor element was not created');
}
});
it('adds that useless bit of xml to the top of the doc if its missing, for reasons no one can explain', async () => {
const doc = new DOMParser().parseFromString(sclString, 'application/xml');
plugin.docs = {
'test.scd': doc,
};
plugin.doc = doc;
await plugin.run();
expect(clickSpy.callCount).to.equal(1);
if (anchorElement && anchorElement.dataset.downloadurl) {
const a = anchorElement as HTMLAnchorElement;
expect(a.download).to.equal('test.scd');
expect(a.href).to.include('blob:');
expect(a.dataset.downloadurl).to.include('application/xml');
const response = await fetch(anchorElement.href);
const blob = await response.blob();
const text = await blob.text();
expect(text.indexOf('<?xml version="1.0" encoding="UTF-8"?>')).to.equal(
0,
);
} else {
throw new Error('Anchor element was not created');
}
});
});