Skip to content

Commit

Permalink
fix: Remove words (#1672)
Browse files Browse the repository at this point in the history
fix: #1671

- Add a fix
- Improve Unit Tests and coverage
  • Loading branch information
Jason3S authored Jan 12, 2022
1 parent 8f2c421 commit bd0e821
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
66 changes: 59 additions & 7 deletions packages/client/src/settings/DictionaryHelper.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
//
import { homedir } from 'os';
import { ConfigurationTarget, Uri, workspace, WorkspaceFolder, ExtensionContext } from 'vscode';
import { ConfigurationTarget, ExtensionContext, Uri, workspace, WorkspaceFolder } from 'vscode';
import { Utils as UriUtils } from 'vscode-uri';
import { CSpellClient } from '../client/client';
import { CSpellUserSettings, CustomDictionaries, CustomDictionaryEntry, DictionaryDefinitionCustom } from '../client';
import { CSpellClient } from '../client/client';
import { getPathToTemp } from '../test/helpers';
import { createConfigFileReaderWriter } from './configFileReadWrite';
import { createCSpellConfigRepository, createVSCodeConfigRepository } from './configRepository';
import { createClientConfigTargetCSpell } from './configTargetHelper';
import { DictionaryHelper, __testing__ } from './DictionaryHelper';
import { createDictionaryTargetForConfigRep } from './DictionaryTarget';
import { MemoryConfigFileReaderWriter, MemoryConfigVSReaderWriter } from './test/memoryReaderWriter';

const {
Expand Down Expand Up @@ -59,17 +61,67 @@ const cfg_1_out: CSpellUserSettings = {
describe('Validate DictionaryHelper', () => {
beforeEach(() => {});

test('isTextDocument', () => {
const uri = Uri.file(__filename);
expect(isTextDocument(uri)).toBe(false);
});

test('DictionaryHelper', () => {
const client = new CSpellClient(fakeExtensionContext, []);
const helper = new DictionaryHelper(client);
expect(helper).toBeDefined();
});

test('addWordsToConfigRep/removeWordsFromConfigRep', async () => {
const uri = getPathToTemp('cspell.json');
const rw = new MemoryConfigFileReaderWriter(uri, {});
const rep = createCSpellConfigRepository(rw);
const client = new CSpellClient(fakeExtensionContext, []);
const helper = new DictionaryHelper(client);

await helper.addWordsToConfigRep(['one', 'two'], rep);

expect(await rep.getValue('words')).toEqual({ words: ['one', 'two'] });

await helper.removeWordsFromConfigRep(['two'], rep);
expect(await rep.getValue('words')).toEqual({ words: ['one'] });
});

test('addWordToDictionaries/removeWordFromDictionaries', async () => {
const uri = getPathToTemp('cspell.json');
const rw = new MemoryConfigFileReaderWriter(uri, {});
const rep = createCSpellConfigRepository(rw);
const client = new CSpellClient(fakeExtensionContext, []);
const helper = new DictionaryHelper(client);
const dict = createDictionaryTargetForConfigRep(rep);

await helper.addWordToDictionaries(['one', 'two'], [dict]);

expect(await rep.getValue('words')).toEqual({ words: ['one', 'two'] });

await helper.removeWordFromDictionaries(['two'], [dict]);
expect(await rep.getValue('words')).toEqual({ words: ['one'] });
});

test('addWordToDictionaries/removeWordFromDictionaries', async () => {
const uri = getPathToTemp('cspell.json');
const target = createClientConfigTargetCSpell(uri, 'unknown');
const rep = createCSpellConfigRepository(uri);
const client = new CSpellClient(fakeExtensionContext, []);
const helper = new DictionaryHelper(client);

await helper.addWordsToTargets(['one', 'two'], [target], undefined);

expect(await rep.getValue('words')).toEqual({ words: ['one', 'two'] });

await helper.removeWordsFromTargets(['two'], [target], undefined);
expect(await rep.getValue('words')).toEqual({ words: ['one'] });
});
});

describe('Validate DictionaryHelper methods', () => {
beforeEach(() => {});

test('isTextDocument', () => {
const uri = Uri.file(__filename);
expect(isTextDocument(uri)).toBe(false);
});

test.each`
cspell | expected
${{}} | ${{ customDictionaries: {} }}
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/settings/DictionaryHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class DictionaryHelper {
public async removeWordFromDictionary(words: string | string[], dictTarget: DictionaryTarget): Promise<void> {
words = normalizeWords(words);
try {
await dictTarget.addWords(words);
await dictTarget.removeWords(words);
} catch (e) {
throw new DictionaryTargetError(`Unable to remove "${words}" from "${dictTarget.name}"`, dictTarget, e);
}
Expand Down

0 comments on commit bd0e821

Please sign in to comment.