我有一个authSlice
const authSlice = createSlice({
name: 'authStore',
initialState,
reducers: {
logout(state = initialState) {
return { ...state, isAuthenticated: false };
},
},
extraReducers: (builder) => {
builder.addCase(login.fulfilled, (state, { payload }) => {
state.isAuthenticated = true;
localStorage.setItem('userId', payload.userId);
});
builder.addCase(login.pending, (state) => {
state.isLoading = true;
});
builder.addCase(login.rejected, (state, { payload, error }) => {
if (payload) {
state.loginError = payload;
state.isLoading = false;
} else {
state.loginError = error;
}
});
},
});
Run Code Online (Sandbox Code Playgroud)
和一个userSlice:
const userSlice …Run Code Online (Sandbox Code Playgroud) 我有一些代码,在一个钩子中,来检测浏览器是否在线/离线:
export function useConnectivity() {
const [isOnline, setNetwork] = useState(window.navigator.onLine);
const updateNetwork = () => {
setNetwork(window.navigator.onLine);
};
useEffect(() => {
window.addEventListener('offline', updateNetwork);
window.addEventListener('online', updateNetwork);
return () => {
window.removeEventListener('offline', updateNetwork);
window.removeEventListener('online', updateNetwork);
};
});
return isOnline;
}
Run Code Online (Sandbox Code Playgroud)
我有这个基本测试:
test('hook should detect offline state', () => {
let internetState = jest.spyOn(window.navigator, 'onLine', 'get');
internetState.mockReturnValue(false);
const { result } = renderHook(() => useConnectivity());
expect(result.current.valueOf()).toBe(false);
});
Run Code Online (Sandbox Code Playgroud)
但是,我想运行一个测试,看看它在offline触发事件时是否返回正确的值,而不仅仅是在渲染时模拟返回值之后。解决这个问题的最佳方法是什么?到目前为止我得到的是这样的:
test('hook should detect offline state then online state', async () => {
const …Run Code Online (Sandbox Code Playgroud) reactjs jestjs react-testing-library react-hooks react-hooks-testing-library
我正在尝试通过getUserMedia在我的 React 应用程序中实现API来访问 iOS 上的相机。
我已经成功地实现了一个在 Chrome 上呈现并正常工作的组件,但是当我mylocalIP:3000在 iOS Safari 上导航时它似乎不起作用。我将非常感谢任何有关我的代码可能出错的地方与 iOS 不兼容的指示。StackBlitz 链接到代码
const Video: React.FC = (props) => {
useEffect(() => {
start()
})
const constraints = {
audio: false,
video: true
};
async function start() {
const stream = await navigator.mediaDevices.getUserMedia(constraints)
const video = document.getElementById('video') as HTMLVideoElement;
video.setAttribute("playsinline", "true");
video.setAttribute("controls", "true");
video.srcObject = stream;
}
return (
<video id='video' autoPlay playsInline></video>
)
}
export default Video
Run Code Online (Sandbox Code Playgroud) 我正在链接到引导程序入门 CSS,并且我有一个个人main.css. 我已经按照入门说明中指定的方式链接了 boostrap,但是我的自定义 css 仅在我将标签<style>放入index.html
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<title>Lunix</title>
</head>
<body>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"
integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
crossorigin="anonymous"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) reactjs ×3
html ×2
typescript ×2
css ×1
jestjs ×1
react-hooks ×1
react-hooks-testing-library ×1
redux ×1
safari ×1