Merge branch 'jest'

This commit is contained in:
balzack 2022-12-31 09:54:21 -08:00
commit babd5efb5b
10 changed files with 9780 additions and 1785 deletions

View File

@ -0,0 +1,6 @@
module.exports = {
preset: "jest-expo",
transform: {
"\\.[jt]sx?$": "babel-jest",
}
};

View File

@ -18,12 +18,15 @@
"@react-navigation/stack": "^6.3.0",
"@stream-io/flat-list-mvcp": "^0.10.2",
"axios": "^1.1.0",
"babel-jest": "^29.3.1",
"crypto-js": "^3.3.0",
"expo": "~46.0.9",
"expo-av": "^12.0.4",
"expo-keep-awake": "~10.2.0",
"expo-splash-screen": "~0.16.2",
"expo-status-bar": "~1.4.0",
"jest": "^29.3.1",
"jest-expo": "^47.0.1",
"jsencrypt": "^3.3.1",
"moment": "^2.29.4",
"react": "18.0.0",
@ -50,7 +53,8 @@
"react-router-native": "^6.3.0"
},
"devDependencies": {
"@babel/core": "^7.12.9"
"@babel/core": "^7.12.9",
"@testing-library/react-native": "^11.5.0"
},
"private": true
}

View File

@ -0,0 +1,149 @@
import React, { useState, useEffect, useContext } from 'react';
import { View, Text } from 'react-native';
import { useTestStoreContext } from './useTestStoreContext.hook';
import {render, act, screen, waitFor, fireEvent} from '@testing-library/react-native'
import { ProfileContextProvider, ProfileContext } from 'context/ProfileContext';
import * as fetchUtil from 'api/fetchUtil';
function ProfileView() {
const profile = useContext(ProfileContext);
return (
<View testID="profile" profile={profile}>
<Text testID="guid">{ profile.state.profile?.guid }</Text>
<Text testID="handle">{ profile.state.profile?.handle }</Text>
<Text testID="name">{ profile.state.profile?.name }</Text>
<Text testID="description">{ profile.state.profile?.description }</Text>
<Text testID="location">{ profile.state.profile?.location }</Text>
<Text testID="image">{ profile.state.profile?.image }</Text>
<Text testID="revision">{ profile.state.profile?.revision }</Text>
<Text testID="seal">{ profile.state.profile?.seal }</Text>
<Text testID="version">{ profile.state.profile?.version }</Text>
<Text testID="node">{ profile.state.profile?.node }</Text>
<Text testID="imageUrl">{ profile.state.imageUrl }</Text>
</View>
);
}
function ProfileTestApp() {
return (
<ProfileContextProvider>
<ProfileView />
</ProfileContextProvider>
)
}
const realUseContext = React.useContext;
const realFetchWithTimeout = fetchUtil.fetchWithTimeout;
const realFetchWithCustomTimeout = fetchUtil.fetchWithCustomTimeout;
let identity = { };
beforeEach(() => {
const mockUseContext = jest.fn().mockImplementation((ctx) => {
return useTestStoreContext();
});
React.useContext = mockUseContext;
const mockFetch = jest.fn().mockImplementation((url, options) => {
if (options.method === 'PUT') {
identity = JSON.parse(options.body);
}
return Promise.resolve({
json: () => Promise.resolve(identity)
});
});
fetchUtil.fetchWithTimeout = mockFetch;
fetchUtil.fetchWithCustomTimeout = mockFetch;
});
afterEach(() => {
React.useContext = realUseContext;
fetchUtil.fetchWithTimeout = realFetchWithTimeout;
fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout;
});
test('testing', async () => {
render(<ProfileTestApp />)
await waitFor(async () => {
expect(screen.getByTestId('name').props.children).toBe(undefined);
});
await act(async () => {
identity = { name: 'jester' };
const profile = screen.getByTestId('profile').props.profile;
await profile.actions.setSession({ guid: 'abc', server: 'test.org', appToken: '123' });
await profile.actions.setRevision(1);
});
await waitFor(async () => {
expect(screen.getByTestId('name').props.children).toBe("jester");
});
await act(async () => {
identity = { name: 'tester' };
const profile = screen.getByTestId('profile').props.profile;
await profile.actions.setRevision(2);
});
await waitFor(async () => {
expect(screen.getByTestId('name').props.children).toBe("tester");
});
await act(async () => {
identity = { name: 'jester' };
const profile = screen.getByTestId('profile').props.profile;
await profile.actions.setRevision(2);
});
await waitFor(async () => {
expect(screen.getByTestId('name').props.children).toBe("tester");
});
await act(async () => {
const profile = screen.getByTestId('profile').props.profile;
await profile.actions.clearSession();
});
await waitFor(async () => {
expect(screen.getByTestId('name').props.children).toBe(undefined);
});
await act(async () => {
identity = { name: 'jester' };
const profile = screen.getByTestId('profile').props.profile;
await profile.actions.setSession({ guid: 'abc', server: 'test.org', appToken: '123' });
});
await waitFor(async () => {
expect(screen.getByTestId('name').props.children).toBe("tester");
expect(screen.getByTestId('imageUrl').props.children).toBe("https://test.org/profile/image?agent=123&revision=2");
});
await act(async () => {
const profile = screen.getByTestId('profile').props.profile;
for (let i = 0; i < 1024; i++) {
identity = { revision: i };
await profile.actions.setRevision(i);
}
});
await waitFor(async () => {
expect(screen.getByTestId('revision').props.children).toBe(1023);
});
await act(async () => {
const profile = screen.getByTestId('profile').props.profile;
await profile.actions.setProfileData("vesper", "sf", "dweb");
await profile.actions.setRevision(1024);
});
await waitFor(async () => {
expect(screen.getByTestId('name').props.children).toBe("vesper");
});
});

View File

@ -0,0 +1,187 @@
import { useEffect, useState, useRef, useContext } from 'react';
import SQLite from "react-native-sqlite-storage";
export function useTestStoreContext() {
const [state, setState] = useState({});
const updateState = (value) => {
setState((s) => ({ ...s, ...value }))
}
const initSession = async (guid) => {
}
const actions = {
init: async () => {
console.log("TEST STORE INIT");
return {};
},
setSession: async (access) => {
},
clearSession: async () => {
},
getProfile: async (guid) => {
return state.profile;
},
setProfile: async (guid, profile) => {
updateState({ profile });
},
getFirstRun: async (guid) => {
},
setFirstRun: async () => {
},
getCardRequestStatus: async (guid) => {
},
setCardRequestStatus: async (guid, status) => {
},
getProfileRevision: async (guid) => {
return state.profileRevision;
},
setProfileRevision: async (guid, revision) => {
updateState({ profileRevision: revision });
},
getAccountStatus: async (guid) => {
},
setAccountStatus: async (guid, status) => {
},
getAccountSealKey: async (guid) => {
},
setAccountSealKey: async (guid, key) => {
},
getAccountRevision: async (guid) => {
},
setAccountRevision: async (guid, revision) => {
},
getCardRevision: async (guid) => {
},
setCardRevision: async (guid, revision) => {
},
setCardItem: async (guid, card) => {
},
clearCardItem: async (guid, cardId) => {
},
setCardItemRevision: async (guid, cardId, revision) => {
},
setCardItemNotifiedView: async (guid, cardId, notified) => {
},
setCardItemNotifiedArticle: async (guid, cardId, notified) => {
},
setCardItemNotifiedProfile: async (guid, cardId, notified) => {
},
setCardItemNotifiedChannel: async (guid, cardId, notified) => {
},
setCardItemOffsync: async (guid, cardId) => {
},
clearCardItemOffsync: async (guid, cardId) => {
},
setCardItemBlocked: async (guid, cardId) => {
},
clearCardItemBlocked: async (guid, cardId) => {
},
setCardItemDetail: async (guid, cardId, revision, detail) => {
},
setCardItemProfile: async (guid, cardId, revision, profile) => {
},
getCardItemStatus: async (guid, cardId) => {
},
getCardItemView: async (guid, cardId) => {
},
getCardItems: async (guid) => {
},
getChannelRevision: async (guid) => {
},
setChannelRevision: async (guid, revision) => {
},
setChannelItem: async (guid, channel) => {
},
clearChannelItem: async (guid, channelId) => {
},
setChannelItemRevision: async (guid, channelId, revision) => {
},
setChannelItemReadRevision: async (guid, channelId, revision) => {
},
setChannelItemSyncRevision: async (guid, channelId, revision) => {
},
setChannelItemTopicMarker: async (guid, channelId, marker) => {
},
setChannelItemBlocked: async (guid, channelId) => {
},
clearChannelItemBlocked: async (guid, channelId) => {
},
setChannelItemDetail: async (guid, channelId, revision, detail) => {
},
setChannelItemUnsealedDetail: async (guid, channelId, revision, unsealed) => {
},
setChannelItemSummary: async (guid, channelId, revision, summary) => {
},
setChannelItemUnsealedSummary: async (guid, channelId, revision, unsealed) => {
},
getChannelItemView: async (guid, channelId) => {
},
getChannelItems: async (guid) => {
},
getChannelTopicItems: async (guid, channelId) => {
},
setChannelTopicItem: async (guid, channelId, topic) => {
},
setChannelTopicItemUnsealedDetail: async (guid, channelId, topicId, revision, unsealed) => {
},
clearChannelTopicItem: async (guid, channelId, topicId) => {
},
clearChannelTopicItems: async (guid, channelId) => {
},
setChannelTopicBlocked: async (guid, channelId, topicId, blocked) => {
},
getChannelTopicBlocked: async (guid) => {
},
setCardChannelItem: async (guid, cardId, channel) => {
},
clearCardChannelItem: async (guid, cardId, channelId) => {
},
setCardChannelItemRevision: async (guid, cardId, channelId, revision) => {
},
setCardChannelItemReadRevision: async (guid, cardId, channelId, revision) => {
},
setCardChannelItemSyncRevision: async (guid, cardId, channelId, revision) => {
},
setCardChannelItemTopicMarker: async (guid, cardId, channelId, marker) => {
},
setCardChannelItemDetail: async (guid, cardId, channelId, revision, detail) => {
},
setCardChannelItemUnsealedDetail: async (guid, cardId, channelId, revision, unsealed) => {
},
setCardChannelItemSummary: async (guid, cardId, channelId, revision, summary) => {
},
setCardChannelItemUnsealedSummary: async (guid, cardId, channelId, revision, unsealed) => {
},
getCardChannelItemView: async (guid, cardId, channelId) => {
},
getCardChannelItems: async (guid) => {
},
clearCardChannelItems: async (guid, cardId) => {
},
getCardChannelTopicItems: async (guid, cardId, channelId) => {
},
setCardChannelTopicItem: async (guid, cardId, channelId, topic) => {
},
setCardChannelTopicItemUnsealedDetail: async (guid, cardId, channelId, topicId, revision, unsealed) => {
},
clearCardChannelTopicItem: async (guid, cardId, channelId, topicId) => {
},
clearCardChannelTopicItems: async (guid, cardId, channelId) => {
},
setCardChannelTopicBlocked: async (guid, cardId, channelId, topicId, blocked) => {
},
getCardChannelTopicBlocked: async (guid) => {
},
}
return { state, actions }
}

File diff suppressed because it is too large Load Diff

6
net/web/babel.config.js Normal file
View File

@ -0,0 +1,6 @@
module.exports = {
presets: [
'@babel/preset-env',
['@babel/preset-react', {runtime: 'automatic'}],
],
};

8468
net/web/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,19 +1,31 @@
{
"name": "antd-demo",
"name": "databag",
"version": "0.1.0",
"private": true,
"jest": {
"testEnvironment": "jsdom",
"modulePaths": [
"<rootDir>/src/"
],
"testMatch": [
"**/test/**"
],
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
"\\.(css|less|scss|sass)$": "identity-obj-proxy"
}
},
"dependencies": {
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^12.0.0",
"@testing-library/user-event": "^13.2.1",
"antd": "^5.0.4",
"axios": "^0.27.2",
"base-64": "^1.0.0",
"crypto-js": "^4.1.1",
"jsencrypt": "^2.3.1",
"react": "^17.0.2",
"react": "^18.2.0",
"react-color": "^2.19.3",
"react-dom": "^17.0.2",
"react-dom": "^18.2.0",
"react-easy-crop": "^4.1.4",
"react-player": "^2.10.0",
"react-resize-detector": "^7.0.0",
@ -45,5 +57,16 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@babel/preset-env": "^7.20.2",
"@babel/preset-react": "^7.18.6",
"@testing-library/dom": "^8.19.1",
"@testing-library/react": "^13.4.0",
"babel-jest": "^29.3.1",
"identity-obj-proxy": "^3.0.0",
"jest": "^29.3.1",
"jest-environment-jsdom": "^29.3.1",
"react-test-renderer": "^18.2.0"
}
}

View File

@ -0,0 +1,65 @@
import React, { useState, useEffect, useContext } from 'react';
import {render, act, screen, waitFor, fireEvent} from '@testing-library/react'
import { ProfileContextProvider, ProfileContext } from 'context/ProfileContext';
import * as fetchUtil from 'api/fetchUtil';
function ProfileView() {
const profile = useContext(ProfileContext);
return (
<div data-testid="profile" profile={profile}>
<span data-testid="guid">{ profile.state.profile?.guid }</span>
<span data-testid="handle">{ profile.state.profile?.handle }</span>
<span data-testid="name">{ profile.state.profile?.name }</span>
<span data-testid="description">{ profile.state.profile?.description }</span>
<span data-testid="location">{ profile.state.profile?.location }</span>
<span data-testid="image">{ profile.state.profile?.image }</span>
<span data-testid="revision">{ profile.state.profile?.revision }</span>
<span data-testid="seal">{ profile.state.profile?.seal }</span>
<span data-testid="version">{ profile.state.profile?.version }</span>
<span data-testid="node">{ profile.state.profile?.node }</span>
</div>
);
}
function ProfileTestApp() {
return (
<ProfileContextProvider>
<ProfileView />
</ProfileContextProvider>
)
}
const realFetchWithTimeout = fetchUtil.fetchWithTimeout;
const realFetchWithCustomTimeout = fetchUtil.fetchWithCustomTimeout;
let identity = { };
beforeEach(() => {
const mockFetch = jest.fn().mockImplementation((url, options) => {
if (options.method === 'PUT') {
identity = JSON.parse(options.body);
}
return Promise.resolve({
json: () => Promise.resolve(identity)
});
});
fetchUtil.fetchWithTimeout = mockFetch;
fetchUtil.fetchWithCustomTimeout = mockFetch;
});
afterEach(() => {
fetchUtil.fetchWithTimeout = realFetchWithTimeout;
fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout;
});
test('testing', async () => {
render(<ProfileTestApp />);
await waitFor(async () => {
expect(screen.getByTestId('name').textContent).toBe("");
});
});

File diff suppressed because it is too large Load Diff