From b8404da8154cae034f3396d6a6d81fbd5361a1af Mon Sep 17 00:00:00 2001 From: Katie Rischpater <98350084+the-bay-kay@users.noreply.github.com> Date: Wed, 18 Oct 2023 12:32:05 -0700 Subject: [PATCH] Set up types folder, added new tests As discussed in PR #1061, Jiji is working on setting up a folder in `www/js/` to contain many of our type interfaces and templates. For now, I've added my own `fileIOTypes.ts`. We discussed the types added so far, and found we were working with overlapping types! As such, I plan to copy over her version of `DiaryTypes.ts` found in commit ffcc871, and will use that to replace the current dataObj interface in `controlHelper.test.ts`. Changes made: - Set up `www/js/types`, moved fsWindow interface there - Added basic tests for createWriteFile --- www/__tests__/controlHelper.test.ts | 73 +++++++++++++++++++++++------ www/js/controlHelper.ts | 15 +----- www/js/types/fileIOTypes.ts | 12 +++++ 3 files changed, 72 insertions(+), 28 deletions(-) create mode 100644 www/js/types/fileIOTypes.ts diff --git a/www/__tests__/controlHelper.test.ts b/www/__tests__/controlHelper.test.ts index 599114676..6842b44bf 100644 --- a/www/__tests__/controlHelper.test.ts +++ b/www/__tests__/controlHelper.test.ts @@ -1,7 +1,9 @@ import { mockLogger } from "../__mocks__/globalMocks"; import { createWriteFile } from "../js/controlHelper"; +import { fsWindow } from "../js/types/fileIOTypes" mockLogger(); +declare let window: fsWindow; // See PR 1052 for a detailed interface interface dataObj { @@ -19,22 +21,24 @@ interface dataObj { } } -// These are fake values; createWriteFile does not require these objects -// specifically, but it is better to test with similar data - using real data -// would take up too much code space, and we cannot use getRawEnteries() in testing -const generateDummyValues = (arraySize: number) => { +// createWriteFile does not require these objects specifically, but it +// is better to test with similar data - using real data would take +// up too much code space, and we cannot use getRawEnteries() in testing +const generateFakeValues = (arraySize: number) => { + if (arraySize <= 0) + return new Promise (() => {return []}); const sampleDataObj = { data: { name: 'MODE', ts: 1234567890.9876543, - reading: 0.1234567891011121 + reading: 0.1234567891011121, }, metadata: 'testValue #', user_id: { - $uuid: '41t0l8e00s914tval1234567u9658699' + $uuid: '41t0l8e00s914tval1234567u9658699', }, _id: { - $oid: '12341x123afe3fbf541524d8' + $oid: '12341x123afe3fbf541524d8', } }; // The parse/stringify lets us "deep copy" the objects, to quickly populate/change the data @@ -43,14 +47,53 @@ const generateDummyValues = (arraySize: number) => { values[index].metadata = element.metadata + index.toString() }); - return values; + return new Promise(() => { + return { phone_data: values }; + }); }; -it(`writes a file for an array of objects`, async () => { - const testPhoneObj = { phone_data: generateDummyValues(100) }; - const writeFile = createWriteFile('testFile.temp'); - const testPromise = new Promise(() => { - return testPhoneObj; - }); - expect(testPromise.then(writeFile)).resolves.not.toThrow(); +// A variation of createShareData; confirms the file has been written, +// without sharing the data. +const confirmFileExists = (fileName: string) => { + return function() { + return new Promise(function() { + window.requestFileSystem(window.LocalFileSystem.TEMPORARY, 0, function(fs) { + fs.root.getFile(fileName, null, function(fileEntry) { + return fileEntry.isFile; + }); + }); + }); + }; +}; + +it('writes a file for an array of objects', async () => { + const testPromiseOne = generateFakeValues(1); + const testPromiseTwo= generateFakeValues(222); + const writeFile = createWriteFile('test_one.temp'); + + expect(testPromiseOne.then(writeFile)).resolves.not.toThrow(); + expect(testPromiseTwo.then(writeFile)).resolves.not.toThrow(); }); + +it('correctly writes the files', async () => { + const fileName = 'test_two.timeline' + const fileExists = confirmFileExists(fileName); + const testPromise = generateFakeValues(1); + const writeFile = createWriteFile(fileName); + expect(testPromise.then(writeFile).then(fileExists)).resolves.not.toThrow(); + expect(testPromise.then(writeFile).then(fileExists)).resolves.toEqual(true); +}); + +it('rejects an empty input', async () => { + const writeFile = createWriteFile('test_one.temp'); + const testPromise = generateFakeValues(0); + expect(testPromise.then(writeFile)).rejects.toThrow(); +}); + +/* + createShareData() is not tested, because it relies on the phoneGap social + sharing plugin, which cannot be mocked. + + getMyData relies on createShareData, and likewise cannot be tested - it also + relies on getRawEnteries(). +*/ diff --git a/www/js/controlHelper.ts b/www/js/controlHelper.ts index c51e58b25..ebdf42398 100644 --- a/www/js/controlHelper.ts +++ b/www/js/controlHelper.ts @@ -2,21 +2,9 @@ import { DateTime } from "luxon"; import { getRawEntries } from "./commHelper"; import { logInfo, displayError, logDebug } from "./plugin/logger"; +import { fsWindow } from "./types/fileIOTypes" import i18next from "./i18nextInit" ; -interface fsWindow extends Window { - requestFileSystem: ( - type: number, - size: number, - successCallback: (fs: any) => void, - errorCallback?: (error: any) => void - ) => void; - LocalFileSystem: { - TEMPORARY: number; - PERSISTENT: number; - }; -}; - declare let window: fsWindow; /** @@ -65,6 +53,7 @@ export const createShareData = function(fileName: string, startTimeString: strin window.requestFileSystem(window.LocalFileSystem.TEMPORARY, 0, function(fs) { logDebug("During email, file system open: " + fs.name); fs.root.getFile(fileName, null, function(fileEntry) { + logDebug(`fileEntry is type ${typeof fileEntry.isFile}`) logDebug(`fileEntry ${fileEntry.nativeURL} is file? ${fileEntry.isFile.toString()}`); fileEntry.file(function(file) { const reader = new FileReader(); diff --git a/www/js/types/fileIOTypes.ts b/www/js/types/fileIOTypes.ts new file mode 100644 index 000000000..12797e6b8 --- /dev/null +++ b/www/js/types/fileIOTypes.ts @@ -0,0 +1,12 @@ +export interface fsWindow extends Window { + requestFileSystem: ( + type: number, + size: number, + successCallback: (fs: any) => void, + errorCallback?: (error: any) => void + ) => void; + LocalFileSystem: { + TEMPORARY: number; + PERSISTENT: number; + }; +};