1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-20 21:21:59 +00:00

Spellcheck - liveSpellcheck

This commit is contained in:
ehhc
2018-06-23 18:15:16 +02:00
parent 82178055af
commit 785272540e
3 changed files with 430 additions and 36 deletions

View File

@@ -8,7 +8,7 @@ beforeEach(() => {
Typo.mockClear()
})
it('should test that checkRange does not marks words that do not contain a typo', function () {
it('should test that checkWord does not marks words that do not contain a typo', function () {
const testWord = 'testWord'
const editor = jest.fn()
editor.getRange = jest.fn(() => testWord)
@@ -18,14 +18,14 @@ it('should test that checkRange does not marks words that do not contain a typo'
mockDictionary.check = jest.fn(() => true)
systemUnderTest.setDictionaryForTestsOnly(mockDictionary)
systemUnderTest.checkRange(editor, range)
systemUnderTest.checkWord(editor, range)
expect(editor.getRange).toHaveBeenCalledWith(range.anchor, range.head)
expect(mockDictionary.check).toHaveBeenCalledWith(testWord)
expect(editor.markText).not.toHaveBeenCalled()
})
it('should test that checkRange should marks words that contain a typo', function () {
it('should test that checkWord should marks words that contain a typo', function () {
const testWord = 'testWord'
const editor = jest.fn()
editor.getRange = jest.fn(() => testWord)
@@ -35,7 +35,7 @@ it('should test that checkRange should marks words that contain a typo', functio
mockDictionary.check = jest.fn(() => false)
systemUnderTest.setDictionaryForTestsOnly(mockDictionary)
systemUnderTest.checkRange(editor, range)
systemUnderTest.checkWord(editor, range)
expect(editor.getRange).toHaveBeenCalledWith(range.anchor, range.head)
expect(mockDictionary.check).toHaveBeenCalledWith(testWord)
@@ -117,7 +117,7 @@ it('should test that checkMultiLineRange performs checks for each word in the st
editor.findWordAt.mockReturnValueOnce(ranges[8])
editor.findWordAt.mockReturnValueOnce(ranges[9])
editor.findWordAt.mockReturnValueOnce(ranges[10])
const checkRangeSpy = jest.spyOn(systemUnderTest, 'checkRange').mockImplementation()
const checkWordSpy = jest.spyOn(systemUnderTest, 'checkWord').mockImplementation()
systemUnderTest.checkMultiLineRange(systemUnderTest, editor, rangeFrom, rangeTo)
@@ -134,18 +134,397 @@ it('should test that checkMultiLineRange performs checks for each word in the st
expect(editor.findWordAt.mock.calls[8][0]).toEqual({line: 3, ch: 0})
expect(editor.findWordAt.mock.calls[9][0]).toEqual({line: 3, ch: 11})
expect(editor.findWordAt.mock.calls[10][0]).toEqual({line: 3, ch: 12})
expect(checkRangeSpy).toHaveBeenCalledTimes(11)
expect(checkRangeSpy.mock.calls[0][1]).toEqual(ranges[0])
expect(checkRangeSpy.mock.calls[1][1]).toEqual(ranges[1])
expect(checkRangeSpy.mock.calls[2][1]).toEqual(ranges[2])
expect(checkRangeSpy.mock.calls[3][1]).toEqual(ranges[3])
expect(checkRangeSpy.mock.calls[4][1]).toEqual(ranges[4])
expect(checkRangeSpy.mock.calls[5][1]).toEqual(ranges[5])
expect(checkRangeSpy.mock.calls[6][1]).toEqual(ranges[6])
expect(checkRangeSpy.mock.calls[7][1]).toEqual(ranges[7])
expect(checkRangeSpy.mock.calls[8][1]).toEqual(ranges[8])
expect(checkRangeSpy.mock.calls[9][1]).toEqual(ranges[9])
expect(checkRangeSpy.mock.calls[10][1]).toEqual(ranges[10])
expect(checkWordSpy).toHaveBeenCalledTimes(11)
expect(checkWordSpy.mock.calls[0][1]).toEqual(ranges[0])
expect(checkWordSpy.mock.calls[1][1]).toEqual(ranges[1])
expect(checkWordSpy.mock.calls[2][1]).toEqual(ranges[2])
expect(checkWordSpy.mock.calls[3][1]).toEqual(ranges[3])
expect(checkWordSpy.mock.calls[4][1]).toEqual(ranges[4])
expect(checkWordSpy.mock.calls[5][1]).toEqual(ranges[5])
expect(checkWordSpy.mock.calls[6][1]).toEqual(ranges[6])
expect(checkWordSpy.mock.calls[7][1]).toEqual(ranges[7])
expect(checkWordSpy.mock.calls[8][1]).toEqual(ranges[8])
expect(checkWordSpy.mock.calls[9][1]).toEqual(ranges[9])
expect(checkWordSpy.mock.calls[10][1]).toEqual(ranges[10])
checkRangeSpy.mockRestore()
checkWordSpy.mockRestore()
})
it('should make sure that liveSpellcheck dont work if the spellcheck is not enabled', function () {
const checkMultiLineRangeSpy = jest.spyOn(systemUnderTest, 'checkMultiLineRange').mockImplementation()
const checkWordSpy = jest.spyOn(systemUnderTest, 'checkWord').mockImplementation()
const editor = jest.fn()
editor.findMarks = jest.fn()
systemUnderTest.setDictionaryForTestsOnly(null)
systemUnderTest.liveSpellcheck(editor, {})
expect(checkWordSpy).not.toHaveBeenCalled()
expect(checkMultiLineRangeSpy).not.toHaveBeenCalled()
expect(editor.findMarks).not.toHaveBeenCalled()
checkWordSpy.mockRestore()
checkMultiLineRangeSpy.mockRestore()
})
it('should make sure that liveSpellcheck works for a range of changes', function () {
const checkMultiLineRangeSpy = jest.spyOn(systemUnderTest, 'checkMultiLineRange').mockImplementation()
const checkWordSpy = jest.spyOn(systemUnderTest, 'checkWord').mockImplementation()
const editor = jest.fn()
editor.findMarks = jest.fn()
const dummyDictionary = jest.fn()
const changeObject = {
from: {line: 2, ch: 0},
to: {line: 7, ch: 13},
text: [
'first line',
'second line',
'third line'
]
}
const expectedFrom = changeObject.from
const expectedTo = {line: 4, ch: 10}
systemUnderTest.setDictionaryForTestsOnly(dummyDictionary)
systemUnderTest.liveSpellcheck(editor, changeObject)
expect(checkWordSpy).not.toHaveBeenCalled()
expect(checkMultiLineRangeSpy).toHaveBeenCalledTimes(1)
expect(checkMultiLineRangeSpy.mock.calls[0][1]).toEqual(editor)
expect(checkMultiLineRangeSpy.mock.calls[0][2]).toEqual(expectedFrom)
expect(checkMultiLineRangeSpy.mock.calls[0][3]).toEqual(expectedTo)
checkWordSpy.mockRestore()
checkMultiLineRangeSpy.mockRestore()
})
it('should make sure that liveSpellcheck works for a range of changes with inverted from to range (due to paste action)', function () {
const checkMultiLineRangeSpy = jest.spyOn(systemUnderTest, 'checkMultiLineRange').mockImplementation()
const checkWordSpy = jest.spyOn(systemUnderTest, 'checkWord').mockImplementation()
const editor = jest.fn()
editor.findMarks = jest.fn()
const dummyDictionary = jest.fn()
const changeObject = {
from: {line: 7, ch: 12},
to: {line: 2, ch: 0},
text: [
'first line',
'second line',
'third line'
]
}
const expectedFrom = changeObject.to
const expectedTo = {line: 4, ch: 10}
systemUnderTest.setDictionaryForTestsOnly(dummyDictionary)
systemUnderTest.liveSpellcheck(editor, changeObject)
expect(checkWordSpy).not.toHaveBeenCalled()
expect(checkMultiLineRangeSpy).toHaveBeenCalledTimes(1)
expect(checkMultiLineRangeSpy.mock.calls[0][1]).toEqual(editor)
expect(checkMultiLineRangeSpy.mock.calls[0][2]).toEqual(expectedFrom)
expect(checkMultiLineRangeSpy.mock.calls[0][3]).toEqual(expectedTo)
checkWordSpy.mockRestore()
checkMultiLineRangeSpy.mockRestore()
})
it('should make sure that liveSpellcheck deletes all existing marks in the given range', function () {
const checkMultiLineRangeSpy = jest.spyOn(systemUnderTest, 'checkMultiLineRange').mockImplementation()
const checkWordSpy = jest.spyOn(systemUnderTest, 'checkWord').mockImplementation()
const editor = jest.fn()
const dummyMarks = [
{clear: jest.fn()},
{clear: jest.fn()},
{clear: jest.fn()}
]
editor.findMarks = jest.fn(() => dummyMarks)
const dummyDictionary = jest.fn()
const changeObject = {
from: {line: 7, ch: 12},
to: {line: 2, ch: 0},
text: [
'first line',
'second line',
'third line'
]
}
const expectedFrom = changeObject.to
const expectedTo = {line: 4, ch: 10}
systemUnderTest.setDictionaryForTestsOnly(dummyDictionary)
systemUnderTest.liveSpellcheck(editor, changeObject)
expect(editor.findMarks).toHaveBeenCalledWith(expectedFrom, expectedTo)
for (const dummyMark of dummyMarks) {
expect(dummyMark.clear).toHaveBeenCalled()
}
checkWordSpy.mockRestore()
checkMultiLineRangeSpy.mockRestore()
})
it('should make sure that liveSpellcheck works when adding a sign', function () {
const checkMultiLineRangeSpy = jest.spyOn(systemUnderTest, 'checkMultiLineRange').mockImplementation()
const checkWordSpy = jest.spyOn(systemUnderTest, 'checkWord').mockImplementation()
const dummyWordRangeResult = {anchor: {line: 33, ch: 33}, head: {line: 33, ch: 34}}
const editor = jest.fn()
editor.findMarks = jest.fn()
editor.findWordAt = jest.fn(() => dummyWordRangeResult)
const dummyDictionary = jest.fn()
const changeObject = {
from: {line: 2, ch: 0},
to: {line: 2, ch: 1},
text: [
'a'
]
}
systemUnderTest.setDictionaryForTestsOnly(dummyDictionary)
systemUnderTest.liveSpellcheck(editor, changeObject)
expect(checkMultiLineRangeSpy).not.toHaveBeenCalled()
expect(checkWordSpy).toHaveBeenCalledTimes(1)
expect(editor.findWordAt).toHaveBeenCalledWith(changeObject.from)
expect(checkWordSpy.mock.calls[0][0]).toEqual(editor)
expect(checkWordSpy.mock.calls[0][1]).toEqual(dummyWordRangeResult)
checkWordSpy.mockRestore()
checkMultiLineRangeSpy.mockRestore()
})
it('should make sure that liveSpellcheck deletes the correct mark when adding a sign', function () {
const checkMultiLineRangeSpy = jest.spyOn(systemUnderTest, 'checkMultiLineRange').mockImplementation()
const checkWordSpy = jest.spyOn(systemUnderTest, 'checkWord').mockImplementation()
const dummyWordRangeResult = {anchor: {line: 33, ch: 33}, head: {line: 33, ch: 34}}
const editor = jest.fn()
const dummyMarks = [
{clear: jest.fn()},
{clear: jest.fn()},
{clear: jest.fn()}
]
editor.findMarks = jest.fn(() => dummyMarks)
editor.findWordAt = jest.fn(() => dummyWordRangeResult)
const dummyDictionary = jest.fn()
const changeObject = {
from: {line: 2, ch: 0},
to: {line: 2, ch: 1},
text: [
'a'
]
}
systemUnderTest.setDictionaryForTestsOnly(dummyDictionary)
systemUnderTest.liveSpellcheck(editor, changeObject)
expect(checkMultiLineRangeSpy).not.toHaveBeenCalled()
expect(checkWordSpy).toHaveBeenCalledTimes(1)
expect(editor.findWordAt).toHaveBeenCalledWith(changeObject.from)
expect(editor.findMarks).toHaveBeenCalledWith(dummyWordRangeResult.anchor, dummyWordRangeResult.head)
for (const dummyMark of dummyMarks) {
expect(dummyMark.clear).toHaveBeenCalled()
}
checkWordSpy.mockRestore()
checkMultiLineRangeSpy.mockRestore()
})
it('should make sure that liveSpellcheck works when deleting a sign', function () {
const checkMultiLineRangeSpy = jest.spyOn(systemUnderTest, 'checkMultiLineRange').mockImplementation()
const checkWordSpy = jest.spyOn(systemUnderTest, 'checkWord').mockImplementation()
const dummyWordRangeResult = {anchor: {line: 33, ch: 33}, head: {line: 33, ch: 34}}
const editor = jest.fn()
editor.findMarks = jest.fn()
editor.findWordAt = jest.fn(() => dummyWordRangeResult)
const dummyDictionary = jest.fn()
const changeObject = {
from: {line: 2, ch: 10},
to: {line: 2, ch: 10},
text: [
''
]
}
const expectedFrom = {line: 2, ch: 9}
systemUnderTest.setDictionaryForTestsOnly(dummyDictionary)
systemUnderTest.liveSpellcheck(editor, changeObject)
expect(checkMultiLineRangeSpy).not.toHaveBeenCalled()
expect(checkWordSpy).toHaveBeenCalledTimes(1)
expect(editor.findWordAt).toHaveBeenCalledWith(expectedFrom)
expect(checkWordSpy.mock.calls[0][0]).toEqual(editor)
expect(checkWordSpy.mock.calls[0][1]).toEqual(dummyWordRangeResult)
checkWordSpy.mockRestore()
checkMultiLineRangeSpy.mockRestore()
})
it('should make sure that liveSpellcheck deletes the right marks when deleting a sign', function () {
const checkMultiLineRangeSpy = jest.spyOn(systemUnderTest, 'checkMultiLineRange').mockImplementation()
const checkWordSpy = jest.spyOn(systemUnderTest, 'checkWord').mockImplementation()
const dummyWordRangeResult = {anchor: {line: 33, ch: 33}, head: {line: 33, ch: 34}}
const editor = jest.fn()
const dummyMarks = [
{clear: jest.fn()},
{clear: jest.fn()},
{clear: jest.fn()}
]
editor.findMarks = jest.fn(() => dummyMarks)
editor.findWordAt = jest.fn(() => dummyWordRangeResult)
const dummyDictionary = jest.fn()
const changeObject = {
from: {line: 2, ch: 10},
to: {line: 2, ch: 10},
text: [
''
]
}
systemUnderTest.setDictionaryForTestsOnly(dummyDictionary)
systemUnderTest.liveSpellcheck(editor, changeObject)
expect(checkMultiLineRangeSpy).not.toHaveBeenCalled()
expect(checkWordSpy).toHaveBeenCalledTimes(1)
expect(editor.findMarks).toHaveBeenCalledWith(dummyWordRangeResult.anchor, dummyWordRangeResult.head)
for (const dummyMark of dummyMarks) {
expect(dummyMark.clear).toHaveBeenCalled()
}
checkWordSpy.mockRestore()
checkMultiLineRangeSpy.mockRestore()
})
it('should make sure that liveSpellcheck works when inserting a space', function () {
const checkMultiLineRangeSpy = jest.spyOn(systemUnderTest, 'checkMultiLineRange').mockImplementation()
const checkWordSpy = jest.spyOn(systemUnderTest, 'checkWord').mockImplementation()
const dummyWordRangeResult = {anchor: {line: 33, ch: 33}, head: {line: 33, ch: 34}}
const editor = jest.fn()
editor.findMarks = jest.fn()
editor.findWordAt = jest.fn(() => dummyWordRangeResult)
const dummyDictionary = jest.fn()
const changeObject = {
from: {line: 2, ch: 10},
to: {line: 2, ch: 10},
text: [
' '
]
}
const expectedFrom = {line: 2, ch: 9}
systemUnderTest.setDictionaryForTestsOnly(dummyDictionary)
systemUnderTest.liveSpellcheck(editor, changeObject)
expect(checkMultiLineRangeSpy).not.toHaveBeenCalled()
expect(checkWordSpy).toHaveBeenCalledTimes(1)
expect(editor.findWordAt).toHaveBeenCalledWith(expectedFrom)
expect(checkWordSpy.mock.calls[0][0]).toEqual(editor)
expect(checkWordSpy.mock.calls[0][1]).toEqual(dummyWordRangeResult)
checkWordSpy.mockRestore()
checkMultiLineRangeSpy.mockRestore()
})
it('should make sure that liveSpellcheck deletes the right marks when inserting a space', function () {
const checkMultiLineRangeSpy = jest.spyOn(systemUnderTest, 'checkMultiLineRange').mockImplementation()
const checkWordSpy = jest.spyOn(systemUnderTest, 'checkWord').mockImplementation()
const dummyWordRangeResult = {anchor: {line: 33, ch: 33}, head: {line: 33, ch: 34}}
const editor = jest.fn()
const dummyMarks = [
{clear: jest.fn()},
{clear: jest.fn()},
{clear: jest.fn()}
]
editor.findMarks = jest.fn(() => dummyMarks)
editor.findWordAt = jest.fn(() => dummyWordRangeResult)
const dummyDictionary = jest.fn()
const changeObject = {
from: {line: 2, ch: 10},
to: {line: 2, ch: 10},
text: [
' '
]
}
systemUnderTest.setDictionaryForTestsOnly(dummyDictionary)
systemUnderTest.liveSpellcheck(editor, changeObject)
expect(checkMultiLineRangeSpy).not.toHaveBeenCalled()
expect(checkWordSpy).toHaveBeenCalledTimes(1)
expect(editor.findMarks).toHaveBeenCalledWith(dummyWordRangeResult.anchor, dummyWordRangeResult.head)
for (const dummyMark of dummyMarks) {
expect(dummyMark.clear).toHaveBeenCalled()
}
checkWordSpy.mockRestore()
checkMultiLineRangeSpy.mockRestore()
})
it('should make sure that liveSpellcheck works when a character is replaced', function () {
const checkMultiLineRangeSpy = jest.spyOn(systemUnderTest, 'checkMultiLineRange').mockImplementation()
const checkWordSpy = jest.spyOn(systemUnderTest, 'checkWord').mockImplementation()
const dummyWordRangeResult = {anchor: {line: 33, ch: 33}, head: {line: 33, ch: 34}}
const editor = jest.fn()
editor.findMarks = jest.fn()
editor.findWordAt = jest.fn(() => dummyWordRangeResult)
const dummyDictionary = jest.fn()
const changeObject = {
from: {line: 2, ch: 9},
to: {line: 2, ch: 8},
text: [
'a'
]
}
const expectedFrom = {line: 2, ch: 8}
systemUnderTest.setDictionaryForTestsOnly(dummyDictionary)
systemUnderTest.liveSpellcheck(editor, changeObject)
expect(checkMultiLineRangeSpy).not.toHaveBeenCalled()
expect(checkWordSpy).toHaveBeenCalledTimes(1)
expect(editor.findWordAt).toHaveBeenCalledWith(expectedFrom)
expect(checkWordSpy.mock.calls[0][0]).toEqual(editor)
expect(checkWordSpy.mock.calls[0][1]).toEqual(dummyWordRangeResult)
checkWordSpy.mockRestore()
checkMultiLineRangeSpy.mockRestore()
})
it('should make sure that liveSpellcheck deletes the right marks when a character is replaced', function () {
const checkMultiLineRangeSpy = jest.spyOn(systemUnderTest, 'checkMultiLineRange').mockImplementation()
const checkWordSpy = jest.spyOn(systemUnderTest, 'checkWord').mockImplementation()
const dummyWordRangeResult = {anchor: {line: 33, ch: 33}, head: {line: 33, ch: 34}}
const editor = jest.fn()
const dummyMarks = [
{clear: jest.fn()},
{clear: jest.fn()},
{clear: jest.fn()}
]
editor.findMarks = jest.fn(() => dummyMarks)
editor.findWordAt = jest.fn(() => dummyWordRangeResult)
const dummyDictionary = jest.fn()
const changeObject = {
from: {line: 2, ch: 9},
to: {line: 2, ch: 8},
text: [
'b'
]
}
systemUnderTest.setDictionaryForTestsOnly(dummyDictionary)
systemUnderTest.liveSpellcheck(editor, changeObject)
expect(checkMultiLineRangeSpy).not.toHaveBeenCalled()
expect(checkWordSpy).toHaveBeenCalledTimes(1)
expect(editor.findMarks).toHaveBeenCalledWith(dummyWordRangeResult.anchor, dummyWordRangeResult.head)
for (const dummyMark of dummyMarks) {
expect(dummyMark.clear).toHaveBeenCalled()
}
checkWordSpy.mockRestore()
checkMultiLineRangeSpy.mockRestore()
})