mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
added web upload context test
This commit is contained in:
parent
132d7cc003
commit
c1a66e2fa7
@ -57,59 +57,65 @@ export function useUploadContext() {
|
||||
}
|
||||
|
||||
const actions = {
|
||||
addTopic: (token, channelId, topicId, files, success, failure) => {
|
||||
const controller = new AbortController();
|
||||
const entry = {
|
||||
index: index.current,
|
||||
url: `/content/channels/${channelId}/topics/${topicId}/assets?agent=${token}`,
|
||||
files,
|
||||
assets: [],
|
||||
current: null,
|
||||
error: false,
|
||||
success,
|
||||
failure,
|
||||
cancel: controller,
|
||||
addTopic: (token, channelId, topicId, files, success, failure, contact) => {
|
||||
if (contact) {
|
||||
const { server, token, cardId } = contact;
|
||||
|
||||
let host = "";
|
||||
if (server) {
|
||||
host = `https://${server}`
|
||||
}
|
||||
const controller = new AbortController();
|
||||
const entry = {
|
||||
index: index.current,
|
||||
url: `${host}/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) });
|
||||
}
|
||||
index.current += 1;
|
||||
const key = `:${channelId}`;
|
||||
if (!channels.current.has(key)) {
|
||||
channels.current.set(key, new Map());
|
||||
else {
|
||||
const controller = new AbortController();
|
||||
const entry = {
|
||||
index: index.current,
|
||||
url: `/content/channels/${channelId}/topics/${topicId}/assets?agent=${token}`,
|
||||
files,
|
||||
assets: [],
|
||||
current: null,
|
||||
error: false,
|
||||
success,
|
||||
failure,
|
||||
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) } );
|
||||
}
|
||||
const topics = channels.current.get(key);
|
||||
topics.set(topicId, entry);
|
||||
upload(entry, updateProgress, () => { updateComplete(key, topicId) } );
|
||||
},
|
||||
cancelTopic: (channelId, topicId) => {
|
||||
abort(`:${channelId}`, topicId);
|
||||
},
|
||||
addContactTopic: (server, token, cardId, channelId, topicId, files, success, failure) => {
|
||||
let host = "";
|
||||
if (server) {
|
||||
host = `https://${server}`
|
||||
cancelTopic: (channelId, topicId, cardId) => {
|
||||
if (cardId) {
|
||||
abort(`${cardId}:${channelId}`, topicId);
|
||||
}
|
||||
const controller = new AbortController();
|
||||
const entry = {
|
||||
index: index.current,
|
||||
url: `${host}/content/channels/${channelId}/topics/${topicId}/assets?contact=${token}`,
|
||||
files,
|
||||
assets: [],
|
||||
current: null,
|
||||
error: false,
|
||||
success,
|
||||
failure,
|
||||
cancel: controller,
|
||||
else {
|
||||
abort(`:${channelId}`, topicId);
|
||||
}
|
||||
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);
|
||||
},
|
||||
clearErrors: (cardId, channelId) => {
|
||||
const key = cardId ? `${cardId}:${channelId}` : `:${channelId}`;
|
||||
|
100
net/web/test/Upload.test.js
Normal file
100
net/web/test/Upload.test.js
Normal file
@ -0,0 +1,100 @@
|
||||
import React, { useState, useEffect, useContext } from 'react';
|
||||
import {render, act, screen, waitFor, fireEvent} from '@testing-library/react'
|
||||
import { UploadContextProvider, UploadContext } from 'context/UploadContext';
|
||||
import axios from 'axios';
|
||||
|
||||
let uploadContext = null;
|
||||
function UploadView() {
|
||||
const [renderCount, setRenderCount] = useState(0);
|
||||
const [total, setTotal] = useState(0);
|
||||
const [channel, setChannel] = useState();
|
||||
const upload = useContext(UploadContext);
|
||||
uploadContext = upload;
|
||||
|
||||
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 (
|
||||
<div>
|
||||
<span data-testid="count">{ renderCount }</span>
|
||||
<span data-testid="channel">{ channel }</span>
|
||||
<span data-testid="total">{ total }</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
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('uploading assets', async () => {
|
||||
let setComplete;
|
||||
render(<UploadTestApp />);
|
||||
|
||||
await waitFor(async () => {
|
||||
expect(uploadContext).not.toBe(null);
|
||||
});
|
||||
|
||||
asset = [ { assetId: '3', transform: 'acopy;audio', status: 'pending' } ];
|
||||
|
||||
setComplete = false;
|
||||
await act(async () => {
|
||||
uploadContext.actions.addTopic('asdf', '123', '1', [{audio: 'asdf'}], ()=>{setComplete=true}, ()=>{});
|
||||
});
|
||||
|
||||
await waitFor(async () => {
|
||||
expect(setComplete).toBe(true);
|
||||
expect(screen.getByTestId('total').textContent).toBe('111');
|
||||
expect(screen.getByTestId('channel').textContent).toBe(':123');
|
||||
});
|
||||
|
||||
setComplete = false;
|
||||
await act(async () => {
|
||||
uploadContext.actions.addTopic('asdf', '123', '1', [{audio: 'asdf'}], ()=>{setComplete=true}, ()=>{}, { server: 'test.org', token: '0011', cardId: '96' });
|
||||
});
|
||||
|
||||
await waitFor(async () => {
|
||||
expect(setComplete).toBe(true);
|
||||
expect(screen.getByTestId('channel').textContent).toBe('96:123');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user