Skip to content
Snippets Groups Projects

Fix front-end tests + refactoring

Merged Fanna Lautenbach requested to merge TMSS-2516 into master
All threads resolved!
Files
2
import React from "react";
import {exportedForTesting} from "../createTemplateSchemaMockFiles"
const fs = require('fs').promises;
const path = require('path');
let replace = require('replace-in-file');
describe('copyJsonFiles', () => {
let mockedCopyFile;
const sourceDir = '/path/to/dir/';
const targetDir = '/target/';
const fileOne = 'wolverine.json';
const fileTwo = 'cyclops.json';
beforeEach(() => {
jest.clearAllMocks();
mockedCopyFile = jest.fn().mockResolvedValue();
fs.mkdir = jest.fn();
fs.copyFile = mockedCopyFile;
});
test('handle an empty directory', async () => {
const mockedReaddir = jest.fn().mockResolvedValue([]);
fs.readdir = mockedReaddir;
fs.lstat = jest.fn();
const results = await exportedForTesting.copyJsonFiles(sourceDir, targetDir);
expect(mockedReaddir).toHaveBeenCalledTimes(1);
expect(mockedReaddir).toHaveBeenCalledWith(sourceDir);
expect(fs.lstat).not.toHaveBeenCalled();
expect(mockedCopyFile).not.toHaveBeenCalled();
expect(results).toEqual([])
});
test('handle file paths only with .json extension', async () => {
let files = [fileOne, 'simba.txt', fileTwo];
const mockedReaddir = jest.fn().mockResolvedValue(files);
const mockedLstat = jest.fn().mockResolvedValue({isDirectory: () => false});
fs.readdir = mockedReaddir;
fs.lstat = mockedLstat;
const results = await exportedForTesting.copyJsonFiles(sourceDir, targetDir);
expect(mockedReaddir).toHaveBeenCalledTimes(1);
expect(mockedReaddir).toHaveBeenCalledWith(sourceDir);
expect(mockedLstat).toHaveBeenCalledTimes(files.length);
files.map(file => sourceDir + file).forEach(filePath => {
expect(mockedLstat).toHaveBeenCalledWith(filePath);
});
expect(mockedCopyFile).toHaveBeenCalledTimes(2);
const targetFileOne = path.join(targetDir, files[0]);
expect(mockedCopyFile).toHaveBeenCalledWith(path.join(sourceDir, files[0]), targetFileOne);
const targetFileTwo = path.join(targetDir, files[2]);
expect(mockedCopyFile).toHaveBeenCalledWith(path.join(sourceDir, files[2]), targetFileTwo);
expect(results).toEqual([targetFileOne, targetFileTwo])
});
test('handles nested directories correctly', async () => {
const subDir = 'x-men';
const mockedReaddir = jest.fn()
.mockResolvedValueOnce([fileOne, subDir])
.mockResolvedValueOnce([fileTwo])
.mockResolvedValueOnce([]);
const mockedLstat = jest.fn()
.mockResolvedValueOnce({isDirectory: () => false})
.mockResolvedValueOnce({isDirectory: () => true})
.mockResolvedValueOnce({isDirectory: () => false});
fs.readdir = mockedReaddir;
fs.lstat = mockedLstat;
const results = await exportedForTesting.copyJsonFiles(sourceDir, targetDir);
expect(mockedReaddir).toHaveBeenCalledTimes(2);
expect(mockedLstat).toHaveBeenCalledTimes(3);
expect(mockedCopyFile).toHaveBeenCalledTimes(2);
const targetFileOne = path.join(targetDir, fileOne);
const targetFileTwo = path.join(targetDir, subDir, fileTwo);
expect(mockedCopyFile).toHaveBeenCalledWith(path.join(sourceDir, fileOne), targetFileOne);
expect(mockedCopyFile).toHaveBeenCalledWith(path.join(sourceDir, subDir, fileTwo), targetFileTwo);
expect(results).toEqual([targetFileOne, targetFileTwo])
});
});
describe('replaceInFile; not testing actual package, only wrapper method', () => {
beforeAll(() => {
// Mock console.error to prevent actual log
jest.spyOn(console, 'error').mockImplementation(() => {
});
});
afterAll(() => {
console.error.mockRestore();
});
test('successfully replaces text in files', async () => {
let fileOneNested = '/x-men/wolverine.json';
let fileTwo = 'cyclops.json';
let mockInput = [{filePath: fileOneNested, hasChanged: true}, {filePath: fileTwo, hasChanged: true}];
const mockReplaceInFile = jest.fn().mockResolvedValueOnce(mockInput);
replace.replaceInFile = mockReplaceInFile
const files = [fileOneNested, fileTwo];
const fromRegEx = /oldText/g;
const to = 'newText';
const result = await exportedForTesting.replaceInFile(files, fromRegEx, to);
expect(result).toEqual(mockInput);
expect(mockReplaceInFile).toHaveBeenCalledTimes(1);
expect(mockReplaceInFile).toHaveBeenCalledWith({files: files, from: fromRegEx, to: to});
});
test('throws error and logs it if replace fails', async () => {
const failMsg = 'X-men heroes failed';
replace.replaceInFile = jest.fn().mockRejectedValueOnce(new Error(failMsg))
const files = ['wolverine.json', 'cyclops.json'];
const fromRegEx = /oldText/g;
const to = 'newText';
let result = exportedForTesting.replaceInFile(files, fromRegEx, to);
await expect(result).rejects.toThrow(failMsg);
expect(console.error).toHaveBeenCalledWith('Error occurred:', expect.any(Error));
});
});
describe("Add JSON elements to template schemas", () => {
const jsonString = '{"name": "Wolverine", "age": 30, "type": "hero", "weapons": []}';
test("adds non extracted data", () => {
const testJson = JSON.parse(jsonString)
const toAddOrUpdate = new Map([['id', 1], ['weapons', ['claws', 'guns']]]);
const resultJson = exportedForTesting.updateJSONSchema(testJson, toAddOrUpdate)
const expectedJson = JSON.parse('{"name": "Wolverine", "age": 30, "type": "hero", "id": 1, "weapons": ["claws", "guns"]}')
expect(resultJson).toEqual(expectedJson)
});
test("adds extracted data successfully", () => {
const testJson = JSON.parse(jsonString)
const toAddOrUpdate = new Map([['weapons', 'weapons']]);
const testExtractionJson = JSON.parse('{"weapons": ["claws", "guns"]}')
const resultJson = exportedForTesting.updateJSONSchema(testJson, toAddOrUpdate, true, testExtractionJson)
const expectedJson = JSON.parse('{"name": "Wolverine", "age": 30, "type": "hero", "weapons": ["claws", "guns"]}')
expect(resultJson).toEqual(expectedJson)
});
test("adds nested extracted data successfully", () => {
const testJson = JSON.parse(jsonString)
const toAddOrUpdate = new Map([['weapons', 'weapons']]);
const testExtractionJson = JSON.parse('{"weapons": {"natural" : ["claws"], "machines": ["guns", "bazooka"]}}')
const resultJson = exportedForTesting.updateJSONSchema(testJson, toAddOrUpdate, true, testExtractionJson)
const expectedJson = JSON.parse('{"name": "Wolverine", "age": 30, "type": "hero", "weapons": {"natural" : ["claws"], "machines": ["guns", "bazooka"]}}')
expect(resultJson).toEqual(expectedJson)
});
test("does not add extracted data when key is missing in extracted json", () => {
const testJson = JSON.parse(jsonString)
const toAddOrUpdate = new Map([['weapons', 'does-not-exist']]);
const testExtractionJson = JSON.parse('{"weapons": ["claws", "guns"]}')
const resultJson = exportedForTesting.updateJSONSchema(testJson, toAddOrUpdate, true, testExtractionJson)
expect(resultJson).toEqual(testJson)
});
});
\ No newline at end of file
Loading