Skip to content

Commit

Permalink
Merge e20cc36 into fbeb243
Browse files Browse the repository at this point in the history
  • Loading branch information
ifirmawan authored Jul 25, 2023
2 parents fbeb243 + e20cc36 commit 7cdef23
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
33 changes: 17 additions & 16 deletions app/src/lib/__test__/cascades.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as FileSystem from 'expo-file-system';
import * as SQLite from 'expo-sqlite';
import { act, waitFor } from 'react-native-testing-library';

import cascades from '../cascades';

Expand All @@ -17,7 +18,7 @@ jest.mock('expo-file-system', () => ({
deleteAsync: jest.fn(async (fileUri) => true),
}));

const DIR_NAME = 'SQLite';
const { DIR_NAME } = cascades;

describe('cascades', () => {
it('should create the sqlite directory if it does not exist', async () => {
Expand All @@ -35,30 +36,30 @@ describe('cascades', () => {
// Call the function with test URLs
const downloadUrl = 'https://example.com/sqlite/file.sqlite';
const fileUrl = '/sqlite/file.sqlite';
const results = await cascades.download(downloadUrl, fileUrl);

// Assertions
expect(FileSystem.getInfoAsync).toHaveBeenCalledWith(
`test-document-directory/${DIR_NAME}/file.sqlite`,
);
expect(FileSystem.downloadAsync).toHaveBeenCalledWith(
downloadUrl,
`test-document-directory/${DIR_NAME}/file.sqlite`,
);
act(() => {
cascades.download(downloadUrl, fileUrl);
});

// Additional assertions on the results, if needed
expect(results.downloadUrl).toBe(downloadUrl);
expect(results.fileUrl).toBe(`test-document-directory/${DIR_NAME}/file.sqlite`);
await waitFor(() => {
// Assertions
expect(FileSystem.getInfoAsync).toHaveBeenCalledWith(
`test-document-directory/${DIR_NAME}/file.sqlite`,
);
expect(FileSystem.downloadAsync).toHaveBeenCalledWith(
downloadUrl,
`test-document-directory/${DIR_NAME}/file.sqlite`,
);
});
});

it('should not download the file if it already exists', async () => {
it('should not download the file if it already exists', () => {
// Mocking that the file already exists
FileSystem.getInfoAsync.mockImplementationOnce(async () => ({ exists: true }));

// Call the function with test URLs
const downloadUrl = 'https://example.com/sqlite/file.sqlite';
const fileUrl = '/sqlite/file.sqlite';
const results = await cascades.download(downloadUrl, fileUrl);
cascades.download(downloadUrl, fileUrl);

// Assertions
expect(FileSystem.getInfoAsync).toHaveBeenCalledWith(
Expand Down
16 changes: 7 additions & 9 deletions app/src/lib/cascades.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ const createSqliteDir = async () => {
}
};

const download = async (downloadUrl, fileUrl) => {
const download = (downloadUrl, fileUrl) => {
const pathSql = fileUrl.replace(/\/sqlite\//, `${DIR_NAME}/`);
// download file if not exists
if (!(await FileSystem.getInfoAsync(FileSystem.documentDirectory + pathSql)).exists) {
const results = await FileSystem.downloadAsync(
downloadUrl,
FileSystem.documentDirectory + pathSql,
);
return results;
}
FileSystem.getInfoAsync(FileSystem.documentDirectory + pathSql).then(({ exists }) => {
if (!exists) {
FileSystem.downloadAsync(downloadUrl, FileSystem.documentDirectory + pathSql);
}
});
};

const loadDataSource = async (source) => {
Expand All @@ -50,4 +47,5 @@ export default cascades = {
loadDataSource,
download,
dropFiles,
DIR_NAME,
};
4 changes: 2 additions & 2 deletions app/src/pages/AuthForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ const AuthForm = ({ navigation }) => {

// download cascades files
if (formRes?.data?.cascades?.length) {
formRes.data.cascades.forEach(async (cascadeFile) => {
formRes.data.cascades.forEach((cascadeFile) => {
const downloadUrl = api.getConfig().baseURL + cascadeFile;
await cascades.download(downloadUrl, cascadeFile);
cascades.download(downloadUrl, cascadeFile);
});
}
});
Expand Down

0 comments on commit 7cdef23

Please sign in to comment.