Skip to content

Commit

Permalink
Merge pull request #1094 from jiji14/prettier-to-service-rewrite
Browse files Browse the repository at this point in the history
Prettier Setup (Merge to Service Rewrite Branch)
  • Loading branch information
shankari authored Nov 2, 2023
2 parents e8075b0 + bb73676 commit 765fa00
Show file tree
Hide file tree
Showing 142 changed files with 12,800 additions and 9,266 deletions.
14 changes: 0 additions & 14 deletions .editorconfig

This file was deleted.

10 changes: 10 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Ignore www/dist, manual_lib, json
www/dist
www/manual_lib
www/json

# This is the pattern to check only www directory
# Ignore all
/*
# but don't ignore all the files in www directory
!/www
10 changes: 10 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"printWidth": 100,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "all",
"bracketSpacing": true,
"bracketSameLine": true,
"endOfLine": "lf",
"semi": true
}
3 changes: 2 additions & 1 deletion package.serve.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"typescript": "^5.0.3",
"url-loader": "^4.1.1",
"webpack": "^5.0.1",
"webpack-cli": "^5.0.1"
"webpack-cli": "^5.0.1",
"prettier": "3.0.3"
},
"dependencies": {
"@react-navigation/native": "^6.1.7",
Expand Down
54 changes: 29 additions & 25 deletions www/__mocks__/cordovaMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,31 @@ export const mockCordova = () => {
window['cordova'].platformId ||= 'ios';
window['cordova'].platformVersion ||= packageJsonBuild.dependencies['cordova-ios'];
window['cordova'].plugins ||= {};
}
};

export const mockDevice = () => {
window['device'] ||= {};
window['device'].platform ||= 'ios';
window['device'].version ||= '14.0.0';
}
};

export const mockGetAppVersion = () => {
const mockGetAppVersion = {
getAppName: () => new Promise((rs, rj) => setTimeout(() => rs('Mock App'), 10)),
getPackageName: () => new Promise((rs, rj) => setTimeout(() => rs('com.example.mockapp'), 10)),
getVersionCode: () => new Promise((rs, rj) => setTimeout(() => rs('123'), 10)),
getVersionNumber: () => new Promise((rs, rj) => setTimeout(() => rs('1.2.3'), 10)),
}
};
window['cordova'] ||= {};
window['cordova'].getAppVersion = mockGetAppVersion;
}
};

export const mockFile = () => {
window['cordova'].file = { "dataDirectory" : "../path/to/data/directory",
"applicationStorageDirectory" : "../path/to/app/storage/directory"};
}
window['cordova'].file = {
dataDirectory: '../path/to/data/directory',
applicationStorageDirectory: '../path/to/app/storage/directory',
};
};

//for consent document
const _storage = {};
Expand All @@ -40,92 +42,94 @@ export const mockBEMUserCache = () => {
return new Promise((rs, rj) =>
setTimeout(() => {
rs(_cache[key]);
}, 100)
}, 100),
);
},
putLocalStorage: (key: string, value: any) => {
return new Promise<void>((rs, rj) =>
setTimeout(() => {
_cache[key] = value;
rs();
}, 100)
}, 100),
);
},
removeLocalStorage: (key: string) => {
return new Promise<void>((rs, rj) =>
setTimeout(() => {
delete _cache[key];
rs();
}, 100)
}, 100),
);
},
clearAll: () => {
return new Promise<void>((rs, rj) =>
setTimeout(() => {
for (let p in _cache) delete _cache[p];
rs();
}, 100)
}, 100),
);
},
listAllLocalStorageKeys: () => {
return new Promise<string[]>((rs, rj) =>
setTimeout(() => {
rs(Object.keys(_cache));
}, 100)
}, 100),
);
},
listAllUniqueKeys: () => {
return new Promise<string[]>((rs, rj) =>
setTimeout(() => {
rs(Object.keys(_cache));
}, 100)
}, 100),
);
},
putMessage: (key: string, value: any) => {
return new Promise<void>((rs, rj) =>
setTimeout(() => {
messages.push({ key, value });
rs();
}, 100)
}, 100),
);
},
getAllMessages: (key: string, withMetadata?: boolean) => {
return new Promise<any[]>((rs, rj) =>
setTimeout(() => {
rs(messages.filter(m => m.key == key).map(m => m.value));
}, 100)
rs(messages.filter((m) => m.key == key).map((m) => m.value));
}, 100),
);
},
getDocument: (key: string, withMetadata?: boolean) => {
return new Promise<any[]>((rs, rj) =>
setTimeout(() => {
rs(_storage[key]);
}, 100)
}, 100),
);
},
isEmptyDoc: (doc) => {
if (doc == undefined) { return true }
if (doc == undefined) {
return true;
}
let string = doc.toString();
if (string.length == 0) {
return true;
} else {
return false;
}
}
}
},
};
window['cordova'] ||= {};
window['cordova'].plugins ||= {};
window['cordova'].plugins.BEMUserCache = mockBEMUserCache;
}
};

export const mockBEMDataCollection = () => {
const mockBEMDataCollection = {
markConsented: (consentDoc) => {
setTimeout(() => {
_storage['config/consent'] = consentDoc;
}, 100)
}
}
}, 100);
},
};
window['cordova'] ||= {};
window['cordova'].plugins.BEMDataCollection = mockBEMDataCollection;
}
};
26 changes: 12 additions & 14 deletions www/__mocks__/fileSystemMocks.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
export const mockFileSystem = () => {
window['resolveLocalFileSystemURL'] = function (parentDir, handleFS) {
const fs = {
filesystem:
{
root:
{
filesystem: {
root: {
getFile: (path, options, onSuccess) => {
let fileEntry = {
file: (handleFile) => {
let file = new File(["this is a mock"], "loggerDB");
let file = new File(['this is a mock'], 'loggerDB');
handleFile(file);
}
}
},
};
onSuccess(fileEntry);
}
}
}
}
console.log("in mock, fs is ", fs, " get File is ", fs.filesystem.root.getFile);
},
},
},
};
console.log('in mock, fs is ', fs, ' get File is ', fs.filesystem.root.getFile);
handleFS(fs);
}
}
};
};
2 changes: 1 addition & 1 deletion www/__mocks__/globalMocks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const mockLogger = () => {
window['Logger'] = { log: console.log };
}
};
27 changes: 10 additions & 17 deletions www/__tests__/LoadMoreButton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
/**
* @jest-environment jsdom
*/
import React from 'react'
import {render, fireEvent, waitFor, screen} from '@testing-library/react-native'
import LoadMoreButton from '../js/diary/list/LoadMoreButton'
import React from 'react';
import { render, fireEvent, waitFor, screen } from '@testing-library/react-native';
import LoadMoreButton from '../js/diary/list/LoadMoreButton';


describe("LoadMoreButton", () => {
it("renders correctly", async () => {
render(
<LoadMoreButton onPressFn={() => {}}>{}</LoadMoreButton>
);
describe('LoadMoreButton', () => {
it('renders correctly', async () => {
render(<LoadMoreButton onPressFn={() => {}}>{}</LoadMoreButton>);
await waitFor(() => {
expect(screen.getByTestId("load-button")).toBeTruthy();
expect(screen.getByTestId('load-button')).toBeTruthy();
});
});

it("calls onPressFn when clicked", () => {
it('calls onPressFn when clicked', () => {
const mockFn = jest.fn();
const { getByTestId } = render(
<LoadMoreButton onPressFn={mockFn}>{}</LoadMoreButton>
);
const loadButton = getByTestId("load-button");
const { getByTestId } = render(<LoadMoreButton onPressFn={mockFn}>{}</LoadMoreButton>);
const loadButton = getByTestId('load-button');
fireEvent.press(loadButton);
expect(mockFn).toHaveBeenCalled();
});
});


16 changes: 11 additions & 5 deletions www/__tests__/clientStats.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { mockBEMUserCache, mockDevice, mockGetAppVersion } from "../__mocks__/cordovaMocks";
import { addStatError, addStatEvent, addStatReading, getAppVersion, statKeys } from "../js/plugin/clientStats";
import { mockBEMUserCache, mockDevice, mockGetAppVersion } from '../__mocks__/cordovaMocks';
import {
addStatError,
addStatEvent,
addStatReading,
getAppVersion,
statKeys,
} from '../js/plugin/clientStats';

mockDevice();
// this mocks cordova-plugin-app-version, generating a "Mock App", version "1.2.3"
Expand All @@ -22,7 +28,7 @@ it('stores a client stats reading', async () => {
ts: expect.any(Number),
reading,
client_app_version: '1.2.3',
client_os_version: '14.0.0'
client_os_version: '14.0.0',
});
});

Expand All @@ -34,7 +40,7 @@ it('stores a client stats event', async () => {
ts: expect.any(Number),
reading: null,
client_app_version: '1.2.3',
client_os_version: '14.0.0'
client_os_version: '14.0.0',
});
});

Expand All @@ -47,6 +53,6 @@ it('stores a client stats error', async () => {
ts: expect.any(Number),
reading: errorStr,
client_app_version: '1.2.3',
client_os_version: '14.0.0'
client_os_version: '14.0.0',
});
});
26 changes: 17 additions & 9 deletions www/__tests__/commHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@ mockLogger();

// mock for JavaScript 'fetch'
// we emulate a 100ms delay when i) fetching data and ii) parsing it as text
global.fetch = (url: string) => new Promise((rs, rj) => {
setTimeout(() => rs({
text: () => new Promise((rs, rj) => {
setTimeout(() => rs('mock data for ' + url), 100);
})
}));
}) as any;
global.fetch = (url: string) =>
new Promise((rs, rj) => {
setTimeout(() =>
rs({
text: () =>
new Promise((rs, rj) => {
setTimeout(() => rs('mock data for ' + url), 100);
}),
}),
);
}) as any;

it('fetches text from a URL and caches it so the next call is faster', async () => {
const tsBeforeCalls = Date.now();
const text1 = await fetchUrlCached('https://raw.githubusercontent.com/e-mission/e-mission-phone/master/README.md');
const text1 = await fetchUrlCached(
'https://raw.githubusercontent.com/e-mission/e-mission-phone/master/README.md',
);
const tsBetweenCalls = Date.now();
const text2 = await fetchUrlCached('https://raw.githubusercontent.com/e-mission/e-mission-phone/master/README.md');
const text2 = await fetchUrlCached(
'https://raw.githubusercontent.com/e-mission/e-mission-phone/master/README.md',
);
const tsAfterCalls = Date.now();
expect(text1).toEqual(expect.stringContaining('mock data'));
expect(text2).toEqual(expect.stringContaining('mock data'));
Expand Down
Loading

0 comments on commit 765fa00

Please sign in to comment.