-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10250 from keymanapp/chore/web/attachmentTests
chore(web): Add unit tests for `pageContextAttachment`
- Loading branch information
Showing
10 changed files
with
677 additions
and
38 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"check-coverage": false, | ||
"clean": true, | ||
"exclude-after-remap": true, | ||
"reporter": ["text", "text-summary"], | ||
"reports-dir": "build/coverage", | ||
"src": [ | ||
"src/" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extensions": ["ts"], | ||
"spec": ["**/*.tests.*"], | ||
"node-option": [ | ||
"experimental-specifier-resolution=node", | ||
"loader=ts-node/esm" | ||
], | ||
"global": "window" | ||
} |
147 changes: 147 additions & 0 deletions
147
web/src/test/auto/headless/engine/attachment/pageContextAttachment.tests.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import { assert } from 'chai'; | ||
import { JSDOM } from 'jsdom'; | ||
import { PageContextAttachment } from 'keyman/engine/attachment'; | ||
|
||
declare global { | ||
namespace NodeJS { | ||
interface Global { | ||
document: Document; | ||
window: Window; | ||
navigator: Navigator; | ||
} | ||
} | ||
} | ||
|
||
describe('PageContextAttachment', () => { | ||
function createDocument(content: string): Document { | ||
const jsdom = new JSDOM(content); | ||
// JSDOM does not support Element.contentEditable | ||
// https://github.com/jsdom/jsdom/issues/1670 | ||
Object.defineProperty(jsdom.window.HTMLElement.prototype, 'contentEditable', { | ||
get: function() { | ||
return this.getAttribute('contenteditable') | ||
} | ||
}); | ||
const { window } = new JSDOM(content); | ||
global.document = window.document; | ||
global.window = global.document.defaultView; | ||
return global.document; | ||
} | ||
|
||
describe('listInputs', () => { | ||
it('empty doc has no elements', () => { | ||
const doc = createDocument('<!doctype html><html><body></body></html>'); | ||
const sut = new PageContextAttachment(doc, null); | ||
sut.listInputs(); | ||
|
||
assert.isEmpty(sut.sortedInputs); | ||
}); | ||
|
||
it('supported input types', () => { | ||
const doc = createDocument( | ||
`<!doctype html><html><body> | ||
<input type="button"> | ||
<input type="checkbox"> | ||
<input type="color"> | ||
<input type="date"> | ||
<input type="datetime-local"> | ||
<input type="email"> | ||
<input type="file"> | ||
<input type="hidden"> | ||
<input type="image"> | ||
<input type="month"> | ||
<input type="number"> | ||
<input type="password"> | ||
<input type="radio"> | ||
<input type="range"> | ||
<input type="reset"> | ||
<input type="search"> | ||
<input type="submit"> | ||
<input type="tel"> | ||
<input type="text"> | ||
<input type="time"> | ||
<input type="url"> | ||
<input type="week"> | ||
<textarea></textarea> | ||
</body></html>`); | ||
const sut = new PageContextAttachment(doc, null); | ||
sut.listInputs(); | ||
|
||
const expected = ['email', 'search', 'text', 'url', 'textarea']; | ||
const types = sut.sortedInputs.map((e: HTMLInputElement) => (e.type)); | ||
assert.equal(types.length, expected.length); | ||
assert.deepEqual(types, expected, `Actual [${types}]`); | ||
}); | ||
|
||
it('ignores disabled', () => { | ||
const doc = createDocument( | ||
`<!doctype html><html><body> | ||
<input type="text" id="1"> | ||
<input type="text" id="2" class="foo kmw-disabled"> | ||
<textarea id="3"></textarea> | ||
<textarea id="4" class="kmw-disabled"></textarea> | ||
</body></html>`); | ||
const sut = new PageContextAttachment(doc, null); | ||
sut.listInputs(); | ||
|
||
const expected = ['1', '3']; | ||
const types = sut.sortedInputs.map((e: HTMLInputElement) => (e.id)); | ||
assert.equal(types.length, expected.length); | ||
assert.deepEqual(types, expected, `Actual [${types}]`); | ||
}); | ||
}); | ||
|
||
describe('isKMWInput', () => { | ||
it('supported input types', () => { | ||
['email', 'search', 'text', 'url'].forEach(function (elementType) { | ||
const doc = createDocument( | ||
`<!doctype html><html><body> | ||
<input type="${elementType}" id="1"> | ||
</body></html>`); | ||
const sut = new PageContextAttachment(doc, null); | ||
const elem = doc.getElementById('1'); | ||
|
||
const result = sut.isKMWInput(elem); | ||
assert.isTrue(result, `${elementType} should be treated as input element`); | ||
}); | ||
}); | ||
|
||
it('unsupported input types', () => { | ||
[ 'button', 'date', 'file', 'hidden', 'image', 'month', 'number', | ||
'password', 'radio', 'range', 'reset', 'submit', 'tel', 'time', | ||
'week'].forEach(function (elementType) { | ||
const doc = createDocument( | ||
`<!doctype html><html><body> | ||
<input type="${elementType}" id="1"> | ||
</body></html>`); | ||
const sut = new PageContextAttachment(doc, null); | ||
const elem = doc.getElementById('1'); | ||
|
||
const result = sut.isKMWInput(elem); | ||
assert.isFalse(result, `${elementType} should not be treated as input element`); | ||
}); | ||
}); | ||
|
||
it('text area is input', () => { | ||
const doc = createDocument( | ||
`<!doctype html><html><body> | ||
<textarea id="1"></textarea> | ||
</body></html>`); | ||
const sut = new PageContextAttachment(doc, null); | ||
const elem = doc.getElementById('1'); | ||
|
||
const result = sut.isKMWInput(elem); | ||
assert.isTrue(result); | ||
}); | ||
|
||
// Can't test contenteditable because JSDOM doesn't support that attribute. | ||
// See https://github.com/jsdom/jsdom/issues/1670 | ||
|
||
// missing tests: | ||
// - iframe without content window -> false | ||
// - iframe with touch and designmode -> false | ||
// - iframe with touch and no designmode -> true | ||
// - iframe with _kmwAttachment -> true | ||
|
||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": "../../tsconfig.dom.json", | ||
|
||
"compilerOptions": { | ||
"baseUrl": "./", | ||
"outDir": "../../../build/test", | ||
"tsBuildInfoFile": "../../../build/test/tsconfig.tsbuildinfo", | ||
"rootDir": "." | ||
}, | ||
|
||
"include": [ "**/*.ts" ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters