adding jest for frontent testing

This commit is contained in:
balzack 2022-12-30 22:16:15 -08:00
parent 11ccf26e56
commit fe271d54c8
5 changed files with 1910 additions and 34 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,70 @@
import React, { useState, useEffect, useContext } from 'react';
import { Text } from 'react-native';
import { useTestStoreContext } from './useTestStoreContext.hook';
import {render, screen, waitFor, fireEvent} from '@testing-library/react-native'
import { ProfileContextProvider, ProfileContext } from 'context/ProfileContext';
import * as fetchUtil from 'api/fetchUtil';
function ProfileTest() {
const [ msg, setMsg ] = useState();
const profile = useContext(ProfileContext);
const testSetup = async () => {
await profile.actions.clearSession();
await profile.actions.setSession({ guid: 'abc', server: 'test.org', appToken: '123' });
await profile.actions.setRevision(1);
setMsg("DONE");
};
useEffect(() => {
testSetup();
}, []);
return (<Text testID="done">{ msg }</Text>);
}
function ProfileTestApp() {
return (
<ProfileContextProvider>
<ProfileTest />
</ProfileContextProvider>
)
}
const realUseContext = React.useContext;
const realFetchWithTimeout = fetchUtil.fetchWithTimeout;
const realFetchWithCustomTimeout = fetchUtil.fetchWithCustomTimeout;
beforeEach(() => {
const mockUseContext = jest.fn().mockImplementation((ctx) => {
return useTestStoreContext();
});
React.useContext = mockUseContext;
const mockFetch = jest.fn().mockImplementation(() => {
return Promise.resolve({
json: () => Promise.resolve({ name: 'jester' })
});
});
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('done').props.children).toBe("DONE");
});
//await new Promise(r => setTimeout(r, 2000));
});

View File

@ -0,0 +1,190 @@
import { useEffect, useState, useRef, useContext } from 'react';
import SQLite from "react-native-sqlite-storage";
export function useTestStoreContext() {
const [state, setState] = useState({});
const db = useRef(null);
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) => {
console.log("GET PROFILE", guid);
return {};
},
setProfile: async (guid, profile) => {
console.log("SET PROFILE", guid, profile);
},
getFirstRun: async (guid) => {
},
setFirstRun: async () => {
},
getCardRequestStatus: async (guid) => {
},
setCardRequestStatus: async (guid, status) => {
},
getProfileRevision: async (guid) => {
console.log("GET PROFILE REVISION", guid);
return 0;
},
setProfileRevision: async (guid, revision) => {
console.log("SET PROFILE REVISION", guid, 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