import path from 'path';
import fse from 'fs-extra';
import { expect } from 'chai';
import { saveJson } from '../../mat/util/file-save';

const BASE_PATH_TMP = './test/tmp';
const BASE_PATH_EXPECTED = './test/data/expected';
const BASE_PATH_SRC = '../data/fixtures';
const FOLDER_NAME = 'util-file-save';

describe('util/file-save.js saveJson()', () => {
    const TMP_SAVE_FOLDER = path.join(BASE_PATH_TMP, FOLDER_NAME);
    const TMP_SAVE_FILE = path.join(TMP_SAVE_FOLDER, 'file.json');

    const EXPECTED_SAVE_FILE = path.join(BASE_PATH_EXPECTED, FOLDER_NAME, 'file.json');
    const SRC_FILE = path.join(BASE_PATH_SRC, FOLDER_NAME, 'file.js');

    before((done) => {
        // 删除临时文件目录
        fse.removeSync(TMP_SAVE_FOLDER);

        saveJson(require(SRC_FILE), TMP_SAVE_FILE)
            .then(() => {
                done();
            })
            .catch((err) => {
                // console.error('err', err);
                done();
            });
    });

    after(() => {
        fse.removeSync(TMP_SAVE_FOLDER);
    });

    it('Save json object to .json file success!', (done) => {
        // 保存的文件内容
        const newSavedFile = fse.readFileSync(TMP_SAVE_FILE, 'utf8');

        // 预期的文件内容
        const expectedFile = fse.readFileSync(EXPECTED_SAVE_FILE, 'utf8');

        expect(newSavedFile).to.equal(expectedFile);

        done();
    });
});

@@ -297,16 +297,45 @@
 e-save';
+%0Aconst FILE_SUFFIX = '.json';
 %0A%0Adescri
@@ -387,31 +387,24 @@
   const 
-TMP_SAVE_FOLDER
+SRC_FILE
  = path.
@@ -418,19 +418,19 @@
 SE_PATH_
-TMP
+SRC
 , FOLDER
@@ -434,16 +434,27 @@
 DER_NAME
+, 'file.js'
 );%0A    c
@@ -462,63 +462,71 @@
 nst 
-TMP_SAVE_FILE = path.join(TMP_SAVE_FOLDER, 'file.json')
+OUTPUT_FILE_NAME = path.basename(SRC_FILE, '.js') + FILE_SUFFIX
 ;%0A%0A 
@@ -534,34 +534,31 @@
   const 
-EXPECTED
+TMP
 _SAVE_F
-ILE
+OLDER
  = path.
@@ -572,24 +572,19 @@
 SE_PATH_
-EXPECTED
+TMP
 , FOLDER
@@ -592,37 +592,106 @@
 NAME
-, 'file.json');%0A    const SRC
+);%0A    const TMP_SAVE_FILE = path.join(TMP_SAVE_FOLDER, OUTPUT_FILE_NAME);%0A    const EXPECTED_SAVE
 _FIL
@@ -706,35 +706,40 @@
 .join(BASE_PATH_
-SRC
+EXPECTED
 , FOLDER_NAME, '
@@ -737,25 +737,32 @@
 R_NAME, 
-'file.js'
+OUTPUT_FILE_NAME
 );%0A%0A    

重构了测试用例的写法，将变量提出来