Skip to content

Commit

Permalink
Set up types folder, added new tests
Browse files Browse the repository at this point in the history
As discussed in PR e-mission#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
  • Loading branch information
the-bay-kay committed Oct 18, 2023
1 parent d31a7a1 commit b8404da
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 28 deletions.
73 changes: 58 additions & 15 deletions www/__tests__/controlHelper.test.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand All @@ -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().
*/
15 changes: 2 additions & 13 deletions www/js/controlHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions www/js/types/fileIOTypes.ts
Original file line number Diff line number Diff line change
@@ -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;
};
};

0 comments on commit b8404da

Please sign in to comment.