|
| 1 | +import { test } from '@japa/runner' |
| 2 | +import { AceFactory } from '../../factories/core/ace.js' |
| 3 | +import LangPublishCommand from '../../commands/lang/publish.js' |
| 4 | + |
| 5 | +test.group('Lang publish', () => { |
| 6 | + test('publish validator messages', async ({ assert, fs }) => { |
| 7 | + const ace = await new AceFactory().make(fs.baseUrl) |
| 8 | + await ace.app.init() |
| 9 | + ace.ui.switchMode('raw') |
| 10 | + |
| 11 | + const command = await ace.create(LangPublishCommand, []) |
| 12 | + await command.exec() |
| 13 | + |
| 14 | + await assert.fileExists('resources/lang/en/validator.json') |
| 15 | + const json = await fs.contentsJson('resources/lang/en/validator.json') |
| 16 | + assert.isObject(json.shared) |
| 17 | + assert.isObject(json.shared.messages) |
| 18 | + assert.isTrue(Object.keys(json.shared.messages).length > 0) |
| 19 | + |
| 20 | + assert.deepEqual(ace.ui.logger.getLogs(), [ |
| 21 | + { |
| 22 | + message: |
| 23 | + '[ green(success) ] Localization template published to resources/lang/en/validator.json', |
| 24 | + stream: 'stdout', |
| 25 | + }, |
| 26 | + ]) |
| 27 | + }) |
| 28 | + |
| 29 | + test('skip when validator messages file already exists', async ({ assert, fs }) => { |
| 30 | + const ace = await new AceFactory().make(fs.baseUrl) |
| 31 | + await ace.app.init() |
| 32 | + ace.ui.switchMode('raw') |
| 33 | + |
| 34 | + await fs.create('resources/lang/en/validator.json', JSON.stringify({ old: true })) |
| 35 | + |
| 36 | + const command = await ace.create(LangPublishCommand, []) |
| 37 | + await command.exec() |
| 38 | + |
| 39 | + const json = await fs.contentsJson('resources/lang/en/validator.json') |
| 40 | + assert.deepEqual(json, { old: true }) |
| 41 | + |
| 42 | + assert.deepEqual(ace.ui.logger.getLogs(), [ |
| 43 | + { |
| 44 | + message: |
| 45 | + '[ red(error) ] File "resources/lang/en/validator.json" already exists. Use "--merge" flag to update the file', |
| 46 | + stream: 'stderr', |
| 47 | + }, |
| 48 | + ]) |
| 49 | + }) |
| 50 | + |
| 51 | + test('merge when validator messages file already exists and --merge is used', async ({ |
| 52 | + assert, |
| 53 | + fs, |
| 54 | + }) => { |
| 55 | + const ace = await new AceFactory().make(fs.baseUrl) |
| 56 | + await ace.app.init() |
| 57 | + ace.ui.switchMode('raw') |
| 58 | + |
| 59 | + await fs.create( |
| 60 | + 'resources/lang/en/validator.json', |
| 61 | + JSON.stringify({ |
| 62 | + shared: { messages: { required: 'foo' } }, |
| 63 | + other: true, |
| 64 | + }) |
| 65 | + ) |
| 66 | + |
| 67 | + const command = await ace.create(LangPublishCommand, ['--merge']) |
| 68 | + await command.exec() |
| 69 | + |
| 70 | + const json = await fs.contentsJson('resources/lang/en/validator.json') |
| 71 | + assert.property(json, 'other') |
| 72 | + assert.equal(json.shared.messages.required, 'foo') |
| 73 | + |
| 74 | + assert.deepEqual(ace.ui.logger.getLogs(), [ |
| 75 | + { |
| 76 | + message: |
| 77 | + '[ green(success) ] Localization template published to resources/lang/en/validator.json', |
| 78 | + stream: 'stdout', |
| 79 | + }, |
| 80 | + ]) |
| 81 | + }) |
| 82 | +}) |
0 commit comments