mirror of
https://github.com/balzack/databag.git
synced 2025-03-13 00:50:03 +00:00
extending web card context test
This commit is contained in:
parent
51d2014aa9
commit
fabb6e3ae9
@ -35,7 +35,6 @@ export function useCardContext() {
|
||||
const setRevision = useRef(null);
|
||||
const curRevision = useRef(null);
|
||||
const cards = useRef(new Map());
|
||||
const forceCard = useRef(null);
|
||||
const force = useRef(false);
|
||||
|
||||
const updateState = (value) => {
|
||||
@ -53,21 +52,19 @@ export function useCardContext() {
|
||||
};
|
||||
|
||||
const resyncCard = async (cardId) => {
|
||||
if (cardId) {
|
||||
forceCard.current = cardId;
|
||||
}
|
||||
if (!syncing.current && forceCard.current) {
|
||||
if (!syncing.current) {
|
||||
syncing.current = true;
|
||||
forceCard.current = null;
|
||||
|
||||
try {
|
||||
const token = access.current;
|
||||
const card = cards.current.get(card.id);
|
||||
const card = cards.current.get(cardId);
|
||||
|
||||
if (card.data.cardDetail.status === 'connected') {
|
||||
await syncCard(token, card);
|
||||
}
|
||||
card.offsync = false;
|
||||
cards.current.set(card.id, card);
|
||||
cards.current.set(cardId, card);
|
||||
updateState({ cards: cards.current });
|
||||
}
|
||||
catch(err) {
|
||||
console.log(err);
|
||||
@ -354,6 +351,9 @@ export function useCardContext() {
|
||||
resync: async () => {
|
||||
await resync();
|
||||
},
|
||||
resyncCard: async (cardId) => {
|
||||
await resyncCard(cardId);
|
||||
},
|
||||
}
|
||||
|
||||
return { state, actions }
|
||||
|
@ -17,7 +17,7 @@ function CardView() {
|
||||
entries.forEach(entry => {
|
||||
|
||||
rendered.push(
|
||||
<div key={entry.id} data-testid="card">
|
||||
<div key={entry.id} data-testid="card" offsync={entry.offsync.toString()}>
|
||||
<span data-testid="name">{ entry.data.cardProfile.name }</span>
|
||||
<span data-testid="status">{ entry.data.cardDetail.status }</span>
|
||||
<span data-testid="token">{ entry.data.cardDetail.token }</span>
|
||||
@ -67,6 +67,7 @@ beforeEach(() => {
|
||||
|
||||
const params = url.split('/');
|
||||
if (params[4]?.split('?')[0] === 'message') {
|
||||
|
||||
return Promise.resolve({
|
||||
url: 'getMessage',
|
||||
status: statusMessage,
|
||||
@ -151,6 +152,74 @@ test('resync cards', async() => {
|
||||
|
||||
});
|
||||
|
||||
test('resync contact', async () => {
|
||||
|
||||
render(<CardTestApp />);
|
||||
|
||||
await waitFor(async () => {
|
||||
expect(cardContext).not.toBe(null);
|
||||
});
|
||||
|
||||
fetchCards = [{
|
||||
id: '000a',
|
||||
revision: 1,
|
||||
data: {
|
||||
detailRevision: 2,
|
||||
profileRevision: 3,
|
||||
notifiedProfile: 3,
|
||||
notifiedArticle: 5,
|
||||
notifiedChannel: 6,
|
||||
notifiedView: 7,
|
||||
cardDetail: { status: 'connected', statusUpdate: 136, token: '01ab', },
|
||||
cardProfile: { guid: '01ab23', handle: 'test1', name: 'tester', imageSet: false,
|
||||
seal: 'abc', version: '1.1.1', node: 'test.org' },
|
||||
},
|
||||
}];
|
||||
|
||||
await act(async () => {
|
||||
cardContext.actions.setToken('abc123');
|
||||
cardContext.actions.setRevision(1);
|
||||
});
|
||||
|
||||
fetchCards = [{
|
||||
id: '000a',
|
||||
revision: 1,
|
||||
data: {
|
||||
detailRevision: 2,
|
||||
profileRevision: 3,
|
||||
notifiedProfile: 4,
|
||||
notifiedArticle: 5,
|
||||
notifiedChannel: 6,
|
||||
notifiedView: 7,
|
||||
},
|
||||
}];
|
||||
|
||||
statusMessage = 500;
|
||||
|
||||
await act(async () => {
|
||||
cardContext.actions.setRevision(2);
|
||||
});
|
||||
|
||||
await waitFor(async () => {
|
||||
expect(screen.getByTestId('card').attributes.offsync.value).toBe('true');
|
||||
});
|
||||
|
||||
statusMessage = 200;
|
||||
|
||||
await act(async () => {
|
||||
cardContext.actions.resyncCard('000a');
|
||||
});
|
||||
|
||||
await waitFor(async () => {
|
||||
expect(screen.getByTestId('card').attributes.offsync.value).toBe('false');
|
||||
});
|
||||
|
||||
act(() => {
|
||||
cardContext.actions.clearToken();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
test('add, update, remove card', async () => {
|
||||
|
||||
render(<CardTestApp />);
|
||||
|
Loading…
Reference in New Issue
Block a user