arb-convert
Version:
Convert Application Resource Bundle (ARB) translation files to other translation formats and back
96 lines (95 loc) • 4.28 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable global-require */
var fs_1 = __importDefault(require("fs"));
var tempy_1 = __importDefault(require("tempy"));
mockDateNow();
var onlySourceXliff = fs_1.default.readFileSync('src/cli/__tests__/only_source.xliff').toString();
var withTargetXliff = fs_1.default.readFileSync('src/cli/__tests__/with_target.xliff').toString();
var withMismatchTargetXliff = fs_1.default.readFileSync('src/cli/__tests__/with_mismatch_target.xliff').toString();
describe('arb2xliff cli tool', function () {
var exitSpy;
var stdoutSpy;
var helpSpy;
beforeAll(function () {
exitSpy = jest.spyOn(process, 'exit').mockImplementation(function () {
throw new Error('process.exit() was called');
});
stdoutSpy = jest.spyOn(process.stdout, 'write').mockImplementation();
});
beforeEach(function () {
// Make require() call work in all tests
jest.resetModules();
exitSpy.mockClear();
stdoutSpy.mockClear();
var Command = require('commander').Command;
helpSpy = jest.spyOn(Command.prototype, 'outputHelp').mockImplementation();
});
test('missing args', function () {
process.argv = ['node', 'arb2xliff.js'];
expect(function () { return require('../arb2xliff'); }).toThrow('process.exit() was called');
expect(exitSpy).toHaveBeenCalledWith(0);
expect(helpSpy).toHaveBeenCalled();
});
test('missing --sourcefile arg', function () {
process.argv = [
'node', 'arb2xliff.js',
'--targetfile', 'foo',
];
expect(function () { return require('../arb2xliff'); }).toThrow('process.exit() was called');
expect(exitSpy).toHaveBeenCalledWith(1);
expect(stdoutSpy).toHaveBeenCalledWith("error: option '--sourcefile <filename>' is required");
expect(helpSpy).not.toHaveBeenCalled();
});
test('with --sourcefile arg to stdout', function () {
process.argv = [
'node', 'arb2xliff.js',
'--sourcefile', 'src/cli/__tests__/source.arb',
];
expect(function () { return require('../arb2xliff'); }).not.toThrow();
expect(exitSpy).not.toHaveBeenCalled();
expect(stdoutSpy).toHaveBeenCalledWith(onlySourceXliff);
expect(helpSpy).not.toHaveBeenCalled();
});
test('with --sourcefile arg to file', function () {
var outPath = tempy_1.default.file({ extension: 'xliff' });
process.argv = [
'node', 'arb2xliff.js',
'--sourcefile', 'src/cli/__tests__/source.arb',
'--out', outPath,
];
expect(function () { return require('../arb2xliff'); }).not.toThrow();
expect(exitSpy).not.toHaveBeenCalled();
expect(stdoutSpy).not.toHaveBeenCalled();
expect(helpSpy).not.toHaveBeenCalled();
expect(fs_1.default.readFileSync(outPath).toString()).toBe(onlySourceXliff);
});
test('with --sourcefile and --targetfile args', function () {
process.argv = [
'node', 'arb2xliff.js',
'--sourcefile', 'src/cli/__tests__/source.arb',
'--targetfile', 'src/cli/__tests__/target_de_DE.arb',
];
expect(function () { return require('../arb2xliff'); }).not.toThrow();
expect(exitSpy).not.toHaveBeenCalled();
expect(stdoutSpy).toHaveBeenCalledWith(withTargetXliff);
expect(helpSpy).not.toHaveBeenCalled();
});
test('with --sourcefile and --targetfile args and mismatch of keys', function () {
process.argv = [
'node', 'arb2xliff.js',
'--sourcefile', 'src/cli/__tests__/source.arb',
'--targetfile', 'src/cli/__tests__/target_mismatch_de_DE.arb',
];
expect(function () { return require('../arb2xliff'); }).not.toThrow();
expect(exitSpy).not.toHaveBeenCalled();
expect(stdoutSpy).toHaveBeenCalledWith(withMismatchTargetXliff);
expect(helpSpy).not.toHaveBeenCalled();
});
afterAll(function () {
jest.restoreAllMocks();
});
});