mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
added test for upload context in mobile app
This commit is contained in:
parent
6c2d1b225d
commit
7e03cdcc97
@ -57,11 +57,15 @@ export function useUploadContext() {
|
||||
}
|
||||
|
||||
const actions = {
|
||||
addTopic: (node, token, channelId, topicId, files, success, failure) => {
|
||||
addTopic: (node, token, channelId, topicId, files, success, failure, cardId) => {
|
||||
const url = cardId ?
|
||||
`https://${node}/content/channels/${channelId}/topics/${topicId}/assets?contact=${token}` :
|
||||
`https://${node}/content/channels/${channelId}/topics/${topicId}/assets?agent=${token}`;
|
||||
const key = cardId ? `${cardId}:${channelId}` : `:${channelId}`;
|
||||
const controller = new AbortController();
|
||||
const entry = {
|
||||
index: index.current,
|
||||
url: `https://${node}/content/channels/${channelId}/topics/${topicId}/assets?agent=${token}`,
|
||||
url: url,
|
||||
files,
|
||||
assets: [],
|
||||
current: null,
|
||||
@ -71,39 +75,17 @@ export function useUploadContext() {
|
||||
cancel: controller,
|
||||
}
|
||||
index.current += 1;
|
||||
const key = `:${channelId}`;
|
||||
if (!channels.current.has(key)) {
|
||||
channels.current.set(key, new Map());
|
||||
}
|
||||
const topics = channels.current.get(key);
|
||||
topics.set(topicId, entry);
|
||||
|
||||
upload(entry, updateProgress, () => { updateComplete(key, topicId) } );
|
||||
},
|
||||
cancelTopic: (channelId, topicId) => {
|
||||
abort(`:${channelId}`, topicId);
|
||||
},
|
||||
addContactTopic: (node, token, cardId, channelId, topicId, files, success, failure) => {
|
||||
const controller = new AbortController();
|
||||
const entry = {
|
||||
index: index.current,
|
||||
url: `https://${node}/content/channels/${channelId}/topics/${topicId}/assets?contact=${token}`,
|
||||
files,
|
||||
assets: [],
|
||||
current: null,
|
||||
error: false,
|
||||
success,
|
||||
failure,
|
||||
cancel: controller,
|
||||
}
|
||||
index.current += 1;
|
||||
const key = `${cardId}:${channelId}`;
|
||||
if (!channels.current.has(key)) {
|
||||
channels.current.set(key, new Map());
|
||||
}
|
||||
const topics = channels.current.get(key);
|
||||
topics.set(topicId, entry);
|
||||
upload(entry, updateProgress, () => { updateComplete(key, topicId) });
|
||||
},
|
||||
cancelContactTopic: (cardId, channelId, topicId) => {
|
||||
abort(`${cardId}:${channelId}`, topicId);
|
||||
},
|
||||
|
94
app/mobile/test/Upload.test.js
Normal file
94
app/mobile/test/Upload.test.js
Normal file
@ -0,0 +1,94 @@
|
||||
import React, { useState, useEffect, useContext } from 'react';
|
||||
import { View, Text } from 'react-native';
|
||||
import {render, act, screen, waitFor, fireEvent} from '@testing-library/react-native'
|
||||
import { UploadContextProvider, UploadContext } from 'context/UploadContext';
|
||||
import axios from 'axios';
|
||||
|
||||
function UploadView() {
|
||||
const [renderCount, setRenderCount] = useState(0);
|
||||
const [total, setTotal] = useState(0);
|
||||
const [channel, setChannel] = useState(0);
|
||||
const upload = useContext(UploadContext);
|
||||
|
||||
useEffect(() => {
|
||||
setRenderCount(renderCount + 1);
|
||||
|
||||
upload.state.progress.forEach((value, key) => {
|
||||
value.forEach(topic => {
|
||||
if (topic.active?.total > total) {
|
||||
setTotal(topic.active?.total);
|
||||
};
|
||||
});
|
||||
setChannel(key);
|
||||
});
|
||||
|
||||
}, [upload.state]);
|
||||
|
||||
return (
|
||||
<View testID="upload" upload={upload} renderCount={renderCount}>
|
||||
<Text testID="channel">{ channel }</Text>
|
||||
<Text testID="total">{ total }</Text>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
function UploadTestApp() {
|
||||
return (
|
||||
<UploadContextProvider>
|
||||
<UploadView />
|
||||
</UploadContextProvider>
|
||||
)
|
||||
}
|
||||
|
||||
const realPost = axios.post;
|
||||
let asset;
|
||||
beforeEach(() => {
|
||||
asset = {};
|
||||
|
||||
const mockPost = jest.fn().mockImplementation(async (url, data, options) => {
|
||||
for (let i = 0; i < 10; i++) {
|
||||
await new Promise(r => setTimeout(r, 10));
|
||||
options.onUploadProgress({ loaded: i * 11, total: 111 });
|
||||
}
|
||||
|
||||
return Promise.resolve({ data: asset });
|
||||
});
|
||||
axios.post = mockPost;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
axios.post = realPost;
|
||||
});
|
||||
|
||||
test('testing', async () => {
|
||||
render(<UploadTestApp />)
|
||||
|
||||
asset = [ { assetId: '3', transform: 'acopy;audio', status: 'pending' } ];
|
||||
|
||||
setComplete = false;
|
||||
await act(async () => {
|
||||
const upload = screen.getByTestId('upload').props.upload;
|
||||
upload.actions.addTopic('test.org', 'asdf', '123', '1', [{ type: 'audio', data: 'asdf'}], ()=>{setComplete=true}, ()=>{});
|
||||
});
|
||||
|
||||
await waitFor(async () => {
|
||||
expect(setComplete).toBe(true);
|
||||
expect(screen.getByTestId('total').props.children).toBe(111);
|
||||
expect(screen.getByTestId('channel').props.children).toBe(':123');
|
||||
});
|
||||
|
||||
setComplete = false;
|
||||
await act(async () => {
|
||||
const upload = screen.getByTestId('upload').props.upload;
|
||||
upload.actions.addTopic('test.org', 'asdf', '123', '1', [{ type: 'audio', data: 'asdf'}], ()=>{setComplete=true}, ()=>{}, '96');
|
||||
});
|
||||
|
||||
await waitFor(async () => {
|
||||
expect(setComplete).toBe(true);
|
||||
expect(screen.getByTestId('channel').props.children).toBe('96:123');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user