2016-03-01 10:47:22 +08:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2017-09-27 02:42:18 +08:00
|
|
|
import { Spellchecker, Message } from 'mailspring-exports';
|
2016-03-01 10:47:22 +08:00
|
|
|
|
|
|
|
import SpellcheckComposerExtension from '../lib/spellcheck-composer-extension';
|
|
|
|
|
2016-03-17 10:27:12 +08:00
|
|
|
const initialPath = path.join(__dirname, 'fixtures', 'california-with-misspellings-before.html');
|
|
|
|
const initialHTML = fs.readFileSync(initialPath).toString();
|
2016-09-24 07:34:09 +08:00
|
|
|
const afterPath = path.join(__dirname, 'fixtures', 'california-with-misspellings-after.html');
|
|
|
|
const afterHTML = fs.readFileSync(afterPath).toString();
|
2016-03-01 10:47:22 +08:00
|
|
|
|
2016-05-05 05:03:15 +08:00
|
|
|
describe('SpellcheckComposerExtension', function spellcheckComposerExtension() {
|
2016-03-17 10:27:12 +08:00
|
|
|
beforeEach(() => {
|
2016-03-01 10:47:22 +08:00
|
|
|
// Avoid differences between node-spellcheck on different platforms
|
2016-03-17 10:27:12 +08:00
|
|
|
const lookupPath = path.join(__dirname, 'fixtures', 'california-spelling-lookup.json');
|
|
|
|
const spellings = JSON.parse(fs.readFileSync(lookupPath));
|
2017-09-27 02:33:08 +08:00
|
|
|
spyOn(Spellchecker, 'isMisspelled').andCallFake(word => spellings[word]);
|
2017-02-10 08:34:53 +08:00
|
|
|
spyOn(Spellchecker.handler, 'provideHintText').andReturn({
|
|
|
|
then(cb) {
|
2017-09-27 02:33:08 +08:00
|
|
|
cb();
|
2017-02-10 08:34:53 +08:00
|
|
|
},
|
2017-09-27 02:33:08 +08:00
|
|
|
});
|
2016-03-01 10:47:22 +08:00
|
|
|
});
|
|
|
|
|
2017-09-27 02:33:08 +08:00
|
|
|
describe('onContentChanged', () => {
|
|
|
|
it('correctly walks a DOM tree and surrounds mispelled words', () => {
|
2016-03-01 10:47:22 +08:00
|
|
|
const node = document.createElement('div');
|
|
|
|
node.innerHTML = initialHTML;
|
|
|
|
|
|
|
|
const editor = {
|
|
|
|
rootNode: node,
|
2017-09-27 02:33:08 +08:00
|
|
|
whilePreservingSelection: cb => cb(),
|
2016-03-01 10:47:22 +08:00
|
|
|
};
|
|
|
|
|
2017-09-27 02:33:08 +08:00
|
|
|
SpellcheckComposerExtension.onContentChanged({ editor });
|
|
|
|
advanceClock(1000); // Wait for debounce
|
|
|
|
advanceClock(1); // Wait for defer
|
2016-09-24 07:34:09 +08:00
|
|
|
expect(node.innerHTML).toEqual(afterHTML);
|
2016-03-01 10:47:22 +08:00
|
|
|
});
|
2016-10-01 02:53:52 +08:00
|
|
|
|
2017-09-27 02:33:08 +08:00
|
|
|
it('does not mark misspelled words inside A, CODE and PRE tags', () => {
|
2016-10-01 02:53:52 +08:00
|
|
|
const node = document.createElement('div');
|
|
|
|
node.innerHTML = `
|
|
|
|
<br>
|
|
|
|
This is a testst! I have a few misspellled words.
|
|
|
|
<code>myvariable</code>
|
|
|
|
<pre>
|
|
|
|
fragmen = document.applieed();
|
|
|
|
</pre>
|
|
|
|
<a href="apple.com">I like appples!</a>
|
|
|
|
<br>
|
|
|
|
This is back to normall.
|
|
|
|
`;
|
|
|
|
|
|
|
|
const editor = {
|
|
|
|
rootNode: node,
|
2017-09-27 02:33:08 +08:00
|
|
|
whilePreservingSelection: cb => cb(),
|
2016-10-01 02:53:52 +08:00
|
|
|
};
|
|
|
|
|
2017-09-27 02:33:08 +08:00
|
|
|
SpellcheckComposerExtension.onContentChanged({ editor });
|
|
|
|
advanceClock(1000); // Wait for debounce
|
|
|
|
advanceClock(1); // Wait for defer
|
2016-10-01 02:53:52 +08:00
|
|
|
expect(node.innerHTML).toEqual(`
|
|
|
|
<br>
|
|
|
|
This is a <spelling class="misspelled">testst</spelling>! I have a few <spelling class="misspelled">misspellled</spelling> words.
|
|
|
|
<code>myvariable</code>
|
|
|
|
<pre> fragmen = document.applieed();
|
|
|
|
</pre>
|
|
|
|
<a href="apple.com">I like appples!</a>
|
|
|
|
<br>
|
|
|
|
This is back to <spelling class="misspelled">normall</spelling>.
|
|
|
|
`);
|
|
|
|
});
|
2017-10-30 08:14:02 +08:00
|
|
|
|
|
|
|
it('handles unicode letters in words correctly', () => {
|
|
|
|
//spyOn(Spellchecker, 'isMisspelled').andCallFake(word => true);
|
|
|
|
const node = document.createElement('div');
|
|
|
|
node.innerHTML = `
|
|
|
|
<br>
|
|
|
|
Schöße, égalité “пустынных”, ಸತ್ಯಾವತಾರ. misspellled.
|
|
|
|
`;
|
|
|
|
|
|
|
|
const editor = {
|
|
|
|
rootNode: node,
|
|
|
|
whilePreservingSelection: cb => cb(),
|
|
|
|
};
|
|
|
|
|
|
|
|
SpellcheckComposerExtension.onContentChanged({ editor });
|
|
|
|
advanceClock(1000); // Wait for debounce
|
|
|
|
advanceClock(1); // Wait for defer
|
|
|
|
expect(node.innerHTML).toEqual(`
|
|
|
|
<br>
|
|
|
|
<spelling class="misspelled">Schöße</spelling>, <spelling class="misspelled">égalité</spelling> “<spelling class="misspelled">пустынных</spelling>”, <spelling class="misspelled">ಸತ್ಯಾವತಾರ</spelling>. <spelling class="misspelled">misspellled</spelling>.
|
|
|
|
`);
|
|
|
|
});
|
2016-03-01 10:47:22 +08:00
|
|
|
});
|
|
|
|
|
2017-09-27 02:33:08 +08:00
|
|
|
describe('applyTransformsForSending', () => {
|
|
|
|
it('removes the spelling annotations it inserted', () => {
|
2016-09-24 07:34:09 +08:00
|
|
|
const draft = new Message({ body: afterHTML });
|
|
|
|
const fragment = document.createDocumentFragment();
|
2017-09-27 02:33:08 +08:00
|
|
|
const draftBodyRootNode = document.createElement('root');
|
|
|
|
fragment.appendChild(draftBodyRootNode);
|
|
|
|
draftBodyRootNode.innerHTML = afterHTML;
|
|
|
|
SpellcheckComposerExtension.applyTransformsForSending({ draftBodyRootNode, draft });
|
2016-09-24 07:34:09 +08:00
|
|
|
expect(draftBodyRootNode.innerHTML).toEqual(initialHTML);
|
2016-03-01 10:47:22 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|