From cefbb69d9c175b3f688e4df57a67e908c25f9832 Mon Sep 17 00:00:00 2001 From: Sam Tolmay Date: Wed, 21 Oct 2020 13:48:58 +0200 Subject: [PATCH] tests(build): add loader and setter tests --- .gitignore | 1 + packages/build/src/context/fileLoader.js | 7 +-- packages/build/src/context/fileLoader.test.js | 51 +++++++++++++++++++ packages/build/src/context/fileSetter.test.js | 43 ++++++++++++++++ .../build/src/test/fileLoader/fileLoader1.txt | 1 + .../build/src/test/fileLoader/fileLoader2.txt | 1 + .../build/src/utils/files/writeFile.test.js | 3 +- 7 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 packages/build/src/context/fileLoader.test.js create mode 100644 packages/build/src/context/fileSetter.test.js create mode 100644 packages/build/src/test/fileLoader/fileLoader1.txt create mode 100644 packages/build/src/test/fileLoader/fileLoader2.txt diff --git a/.gitignore b/.gitignore index ecc6322ed..d046f3661 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ packages/express/config/** packages/build/src/test/writeFile.txt +packages/build/src/utils/files/writeFile.test.js diff --git a/packages/build/src/context/fileLoader.js b/packages/build/src/context/fileLoader.js index 075cc5e67..d80b4389c 100644 --- a/packages/build/src/context/fileLoader.js +++ b/packages/build/src/context/fileLoader.js @@ -21,13 +21,14 @@ import getFile from '../utils/files/getFile'; function createFileBatchLoader({ baseDir }) { async function loader(keys) { + const filePaths = keys.map((key) => path.resolve(baseDir, key)); const fetched = []; - const promises = keys.map(async (filePath) => { - const item = await getFile(path.resolve(baseDir, filePath)); + const promises = filePaths.map(async (filePath) => { + const item = await getFile(filePath); fetched.push(item); }); await Promise.all(promises); - const returned = keys + const returned = filePaths .map((filePath) => fetched.find((item) => { return get(item, 'filePath') === filePath; diff --git a/packages/build/src/context/fileLoader.test.js b/packages/build/src/context/fileLoader.test.js new file mode 100644 index 000000000..3a0bad80f --- /dev/null +++ b/packages/build/src/context/fileLoader.test.js @@ -0,0 +1,51 @@ +/* + Copyright 2020 Lowdefy, Inc + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +import path from 'path'; +import createFileLoader from './fileLoader'; + +const baseDir = path.resolve(process.cwd(), 'src/test/fileLoader'); + +test('load file', async () => { + const fileLoader = createFileLoader({ baseDir }); + const res = await fileLoader.load('fileLoader1.txt'); + expect(res).toEqual('File loader text file 1.'); +}); + +test('load two files', async () => { + const fileLoader = createFileLoader({ baseDir }); + const files = ['fileLoader1.txt', 'fileLoader2.txt']; + const res = await Promise.all(files.map((file) => fileLoader.load(file))); + expect(res).toEqual(['File loader text file 1.', 'File loader text file 2.']); +}); + +test('load two files, one file errors', async () => { + const fileLoader = createFileLoader({ baseDir }); + const files = ['fileLoader1.txt', 'doesNotExist.txt']; + await expect(Promise.all(files.map((file) => fileLoader.load(file)))).rejects.toThrow( + 'src/test/fileLoader/doesNotExist.txt", but file does not exist' + ); +}); + +test('load file, file does not exist', async () => { + const filePath = path.resolve(baseDir, 'doesNotExist.txt'); + const fileLoader = createFileLoader({ baseDir }); + // Since error message contains exact file path, test if parts of error message are present + await expect(fileLoader.load(filePath)).rejects.toThrow('Tried to read file with file path'); + await expect(fileLoader.load(filePath)).rejects.toThrow( + 'src/test/fileLoader/doesNotExist.txt", but file does not exist' + ); +}); diff --git a/packages/build/src/context/fileSetter.test.js b/packages/build/src/context/fileSetter.test.js new file mode 100644 index 000000000..356cca52a --- /dev/null +++ b/packages/build/src/context/fileSetter.test.js @@ -0,0 +1,43 @@ +/* + Copyright 2020 Lowdefy, Inc + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +import fs from 'fs'; +import path from 'path'; +import createFileSetter from './fileSetter'; + +const baseDir = path.resolve(process.cwd(), 'src/test/fileSetter'); + +test('writeFile', async () => { + const filePath = path.resolve(baseDir, 'writeFile.txt'); + try { + fs.unlinkSync(filePath); + } catch (error) { + //pass + } + expect(fs.existsSync(filePath)).toBe(false); + const fileSetter = createFileSetter({ baseDir }); + await fileSetter.set({ + filePath: 'writeFile.txt', + content: 'Test fileSetter file', + }); + const res = fs.readFileSync(filePath, 'utf8'); + expect(res).toEqual('Test fileSetter file'); + try { + fs.unlinkSync(filePath); + } catch (error) { + //pass + } +}); diff --git a/packages/build/src/test/fileLoader/fileLoader1.txt b/packages/build/src/test/fileLoader/fileLoader1.txt new file mode 100644 index 000000000..09d3dcc70 --- /dev/null +++ b/packages/build/src/test/fileLoader/fileLoader1.txt @@ -0,0 +1 @@ +File loader text file 1. \ No newline at end of file diff --git a/packages/build/src/test/fileLoader/fileLoader2.txt b/packages/build/src/test/fileLoader/fileLoader2.txt new file mode 100644 index 000000000..9af91bc35 --- /dev/null +++ b/packages/build/src/test/fileLoader/fileLoader2.txt @@ -0,0 +1 @@ +File loader text file 2. \ No newline at end of file diff --git a/packages/build/src/utils/files/writeFile.test.js b/packages/build/src/utils/files/writeFile.test.js index 7adb109ed..a16581817 100644 --- a/packages/build/src/utils/files/writeFile.test.js +++ b/packages/build/src/utils/files/writeFile.test.js @@ -17,7 +17,6 @@ import fs from 'fs'; import path from 'path'; import writeFile from './writeFile'; -import readFile from './readFile'; const baseDir = path.resolve(process.cwd(), 'src/test/getFile'); @@ -33,7 +32,7 @@ test('writeFile', async () => { filePath, content: `Test Write File`, }); - const res = await readFile(filePath); + const res = fs.readFileSync(filePath, 'utf8'); expect(res).toEqual(`Test Write File`); try { fs.unlinkSync(filePath);