WXL
5 天以前 871522ed7e06fd9c62a87c178d7f5c88d7853a20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import path from 'path';
import { reportCommand, reportFromConfigCommand } from '@/report-command';
import { ReportOptions } from '@/types';
import { expectedI18NReport } from '../fixtures/expected-values';
import { vueFiles, languageFiles } from '../fixtures/resolved-sources';
import * as report from '@/report-command/report';
import * as languageFileActions from '@/report-command/language-files';
 
describe('file: report-command/index', () => {
  describe('function: reportCommand', () => {
    let consoleTableSpy: jest.SpyInstance<unknown>;
    let consoleLogSpy: jest.SpyInstance<unknown>;
    let consoleInfoSpy: jest.SpyInstance<unknown>;
    let command: ReportOptions;
 
 
    beforeEach(() => {
      command = {
        vueFiles,
        languageFiles,
      }
 
      consoleTableSpy = jest.spyOn(console, 'table');
      consoleLogSpy = jest.spyOn(console, 'log');
      consoleInfoSpy = jest.spyOn(console, 'info');
      consoleTableSpy.mockImplementation(() => jest.fn());
      consoleLogSpy.mockImplementation(() => jest.fn());
      consoleInfoSpy.mockImplementation(() => jest.fn());
    });
 
    it('Log report to console', () => {
      reportCommand(command);
      expect(consoleTableSpy).toHaveBeenCalledTimes(2);
      expect(consoleTableSpy.mock.calls[0][0]).toEqual(expectedI18NReport.missingKeys);
      expect(consoleTableSpy.mock.calls[1][0]).toEqual(expectedI18NReport.unusedKeys);
    });
 
    it('Write report to file at output path', async () => {
      command.output = './test';
      const writeReportSpy: jest.SpyInstance<unknown> = jest.spyOn(report, 'writeReportToFile');
      writeReportSpy.mockImplementation(() => jest.fn());
      await reportCommand(command);
      expect(writeReportSpy).toHaveBeenCalledWith(
        expectedI18NReport,
        path.resolve(process.cwd(), command.output),
      );
      expect(consoleLogSpy).toHaveBeenCalledWith(`The report has been has been saved to ${command.output}`)
    });
 
    it('Write missing keys to language files', async () => {
      command.add = true;
      const addMissingSpy: jest.SpyInstance<unknown> = jest.spyOn(
        languageFileActions.LanguageFileUpdater.prototype, 'addMissingKeys'
      );
      const writeChangesSpy: jest.SpyInstance<unknown> = jest.spyOn(
        languageFileActions.LanguageFileUpdater.prototype, 'writeChanges'
      );
      writeChangesSpy.mockImplementation(() => jest.fn());
      await reportCommand(command);
      expect(addMissingSpy).toHaveBeenCalledWith(expectedI18NReport.missingKeys);
      expect(writeChangesSpy).toHaveBeenCalled();
      expect(consoleLogSpy).toHaveBeenCalledWith(`The missing keys have been added`);
      expect(consoleLogSpy).toHaveBeenCalledWith(`Language files have been updated`);
    });
  });
});