|
| 1 | +import * as yargs from 'yargs' |
| 2 | +import * as path from 'path' |
| 3 | + |
| 4 | +// ------ |
| 5 | +// mocks |
| 6 | + |
| 7 | +// l10n service provider module |
| 8 | +const mockPull = jest.fn() |
| 9 | +jest.mock('@scope/l10n-service-provider', () => { |
| 10 | + return jest.fn().mockImplementation(() => { |
| 11 | + return { pull: mockPull } |
| 12 | + }) |
| 13 | +}) |
| 14 | +import L10nServiceProvider from '@scope/l10n-service-provider' |
| 15 | + |
| 16 | +// fs module |
| 17 | +jest.mock('fs', () => ({ |
| 18 | + ...jest.requireActual('fs'), |
| 19 | + mkdir: jest.fn(), |
| 20 | + writeFile: jest.fn() |
| 21 | +})) |
| 22 | +import fs from 'fs' |
| 23 | + |
| 24 | +// -------------------- |
| 25 | +// setup/teadown hooks |
| 26 | + |
| 27 | +let spyLog |
| 28 | +let spyError |
| 29 | +beforeEach(() => { |
| 30 | + spyLog = jest.spyOn(global.console, 'log') |
| 31 | + spyError = jest.spyOn(global.console, 'error') |
| 32 | +}) |
| 33 | + |
| 34 | +afterEach(() => { |
| 35 | + spyError.mockRestore() |
| 36 | + spyLog.mockRestore() |
| 37 | + jest.clearAllMocks() |
| 38 | +}) |
| 39 | + |
| 40 | +// ----------- |
| 41 | +// test cases |
| 42 | + |
| 43 | +test('require options', async () => { |
| 44 | + const pull = await import('../../src/commands/pull') |
| 45 | + const cmd = yargs.command(pull) |
| 46 | + try { |
| 47 | + await new Promise((resolve, reject) => { |
| 48 | + cmd.parse(`pull`, (err, argv, output) => { |
| 49 | + err ? reject(err) : resolve(output) |
| 50 | + }) |
| 51 | + }) |
| 52 | + } catch (e) { |
| 53 | + expect(e).toMatchObject({ name: 'YError' }) |
| 54 | + } |
| 55 | +}) |
| 56 | + |
| 57 | +test('--provider: not found', async () => { |
| 58 | + const pull = await import('../../src/commands/pull') |
| 59 | + const cmd = yargs.command(pull) |
| 60 | + await new Promise((resolve, reject) => { |
| 61 | + cmd.parse(`pull --provider=./404-provider.js \ |
| 62 | + --output=./foo`, (err, argv, output) => { |
| 63 | + err ? reject(err) : resolve(output) |
| 64 | + }) |
| 65 | + }) |
| 66 | + expect(spyLog).toHaveBeenCalledWith('Not found ./404-provider.js provider') |
| 67 | +}) |
| 68 | + |
| 69 | +test('--conf option', async () => { |
| 70 | + // setup mocks |
| 71 | + mockPull.mockImplementation(locales => Promise.resolve({ ja: {}, en: {}})) |
| 72 | + |
| 73 | + // run |
| 74 | + const pull = await import('../../src/commands/pull') |
| 75 | + const cmd = yargs.command(pull) |
| 76 | + await new Promise((resolve, reject) => { |
| 77 | + cmd.parse(`pull --provider=@scope/l10n-service-provider \ |
| 78 | + --conf=./test/fixtures/conf/l10n-service-provider-conf.json \ |
| 79 | + --output=./test/fixtures/locales \ |
| 80 | + --dry-run`, (err, argv, output) => { |
| 81 | + err ? reject(err) : resolve(output) |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + expect(L10nServiceProvider).toHaveBeenCalledWith({ |
| 86 | + provider: { token: 'xxx' }, |
| 87 | + pushMode: 'file-path' |
| 88 | + }) |
| 89 | +}) |
| 90 | + |
| 91 | +test('--locales option', async () => { |
| 92 | + // setup mocks |
| 93 | + mockPull.mockImplementation(locales => Promise.resolve({ ja: {}, en: {}})) |
| 94 | + |
| 95 | + // run |
| 96 | + const pull = await import('../../src/commands/pull') |
| 97 | + const cmd = yargs.command(pull) |
| 98 | + await new Promise((resolve, reject) => { |
| 99 | + cmd.parse(`pull --provider=@scope/l10n-service-provider \ |
| 100 | + --output=./test/fixtures/locales \ |
| 101 | + --locales=en,ja,fr \ |
| 102 | + --dry-run`, (err, argv, output) => { |
| 103 | + err ? reject(err) : resolve(output) |
| 104 | + }) |
| 105 | + }) |
| 106 | + |
| 107 | + expect(mockPull).toHaveBeenCalledWith(['en', 'ja', 'fr'], true) |
| 108 | +}) |
| 109 | + |
| 110 | +test('--output option', async () => { |
| 111 | + // setup mocks |
| 112 | + mockPull.mockImplementation(locales => Promise.resolve({ ja: { hello: 'hello' }, en: { hello: 'こんにちわわわ!' }})) |
| 113 | + const mockFS = fs as jest.Mocked<typeof fs> |
| 114 | + mockFS.mkdir.mockImplementation((p, option, cb) => cb(null)) |
| 115 | + mockFS.writeFile.mockImplementation((p, data, cb) => cb(null)) |
| 116 | + |
| 117 | + // run |
| 118 | + const OUTPUT_FULL_PATH = path.resolve('./test/fixtures/locales') |
| 119 | + const pull = await import('../../src/commands/pull') |
| 120 | + const cmd = yargs.command(pull) |
| 121 | + await new Promise((resolve, reject) => { |
| 122 | + cmd.parse(`pull --provider=@scope/l10n-service-provider \ |
| 123 | + --conf=./test/fixtures/conf/l10n-service-provider-conf.json \ |
| 124 | + --output=./test/fixtures/locales`, (err, argv, output) => { |
| 125 | + err ? reject(err) : resolve(output) |
| 126 | + }) |
| 127 | + }) |
| 128 | + |
| 129 | + expect(mockFS.mkdir.mock.calls[0][0]).toEqual(OUTPUT_FULL_PATH) |
| 130 | + expect(mockFS.mkdir.mock.calls[0][1]).toEqual({ recursive: true }) |
| 131 | +}) |
0 commit comments