Skip to content

Commit

Permalink
refactor(translator): use fs.readFileSync instead of require
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasremdt committed Jul 30, 2020
1 parent 3422adb commit 6cff147
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 59 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.0] - 2020-07-30
## [2.0.0] - 2020-07-29

### Breaking changes

Expand Down
4 changes: 3 additions & 1 deletion src/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,9 @@ class Translator {

urls.forEach((url, index) => {
try {
const json = require(process.cwd() + url);
const json = JSON.parse(
require('fs').readFileSync(process.cwd() + url, 'utf-8')
);

if (save) {
this.add(sources[index], json);
Expand Down
116 changes: 59 additions & 57 deletions tests/translator.node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@

import Translator from '../src/translator.js';

// jest.mock(process.cwd() + '/i18n/de.json', () => {
// return { title: 'Deutscher Titel', paragraph: 'Hallo Welt' };
// });

// jest.mock(process.cwd() + '/i18n/en.json', () => {
// return { title: 'English title', paragraph: 'Hello World' };
// });

describe('constructor()', () => {
let translator;

Expand Down Expand Up @@ -69,52 +61,62 @@ describe('translatePageTo()', () => {
});
});

// describe('fetch()', () => {
// let translator;
// let consoleSpy;
// const RESOURCE_FILES = {
// de: { title: 'Deutscher Titel', paragraph: 'Hallo Welt' },
// en: { title: 'English title', paragraph: 'Hello World' },
// };

// beforeEach(() => {
// translator = new Translator({ debug: true });
// consoleSpy = jest.spyOn(global.console, 'error').mockImplementation();
// });

// afterEach(() => {
// translator = null;
// jest.clearAllMocks();
// delete global.fetch;
// });

// it('fetches a single resource using cjs', (done) => {
// translator.fetch('de').then((value) => {
// expect(value).toMatchObject(RESOURCE_FILES['de']);
// expect(translator.languages.size).toBe(1);
// expect(translator.languages.get('de')).toMatchObject(
// RESOURCE_FILES['de']
// );
// done();
// });
// });

// it('fetches a multiple resources', (done) => {
// translator.fetch(['de', 'en']).then((value) => {
// expect(value).toMatchObject([RESOURCE_FILES['de'], RESOURCE_FILES['en']]);
// expect(translator.languages.size).toBe(2);
// done();
// });
// });

// it("displays an error when the resource doesn't exist", (done) => {
// translator.fetch('nl').then((value) => {
// expect(value).toBeUndefined();
// expect(consoleSpy).toHaveBeenCalledTimes(1);
// expect(consoleSpy).toHaveBeenCalledWith(
// expect.stringContaining('MODULE_NOT_FOUND')
// );
// done();
// });
// });
// });
describe('fetch()', () => {
let translator;
let consoleSpy;
const RESOURCE_FILES = {
de: { title: 'Deutscher Titel', paragraph: 'Hallo Welt' },
en: { title: 'English title', paragraph: 'Hello World' },
};

jest.mock('fs', () => ({
readFileSync: jest.fn((url) => {
if (url.includes('de.json')) {
return JSON.stringify({
title: 'Deutscher Titel',
paragraph: 'Hallo Welt',
});
} else if (url.includes('en.json')) {
return JSON.stringify({
title: 'English title',
paragraph: 'Hello World',
});
}
}),
}));

beforeEach(() => {
translator = new Translator({ debug: true });
consoleSpy = jest.spyOn(global.console, 'error').mockImplementation();
});

it('fetches a single resource using cjs', (done) => {
translator.fetch('de').then((value) => {
expect(value).toMatchObject(RESOURCE_FILES['de']);
expect(translator.languages.size).toBe(1);
expect(translator.languages.get('de')).toMatchObject(
RESOURCE_FILES['de']
);
done();
});
});

it('fetches a multiple resources', (done) => {
translator.fetch(['de', 'en']).then((value) => {
expect(value).toMatchObject([RESOURCE_FILES['de'], RESOURCE_FILES['en']]);
expect(translator.languages.size).toBe(2);
done();
});
});

it("displays an error when the resource doesn't exist", (done) => {
translator.fetch('nl').then((value) => {
expect(value).toBeUndefined();
expect(consoleSpy).toHaveBeenCalledTimes(1);
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('MODULE_NOT_FOUND')
);
done();
});
});
});

0 comments on commit 6cff147

Please sign in to comment.