如何使用 jest 在 React 测试中处理 localStorage。
我已添加到setupTest.js
const localStorageMock = {
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn(),
clear: jest.fn(),
};
global.localStorage = localStorageMock;
Run Code Online (Sandbox Code Playgroud)
这是我获取缓存的服务的示例:
export default class Cache {
getCache = () => {
return Promise.resolve(JSON.parse(localStorage.getItem('cache')));
}
}
Run Code Online (Sandbox Code Playgroud)
样本.test.js
import { mount } from 'enzyme';
import React from 'react';
import Component from '../Component';
const cacheData = JSON.stringify(require('../__mock__/data.json'));
// Trying to mock the function causing error _Cache.default .....
jest.mock('./Cache', () => {
return jest.fn().mockImplementation(() => {
return {
getCache: () => …Run Code Online (Sandbox Code Playgroud) 如何使用Image.memory()或MemoryImage()flutter缓存内存图像?
我有数据列表,但图像类型是字节。我正在使用Image.memory()或MemoryImage()内部,Gridview.builder但当分页(新数据即将到来)或滚动时,它每次重新渲染网格示例都会闪烁。
感谢帮助
代码:
StreamBuilder<List<Datum>>(
stream: _provider.promotionsStream,
builder: (BuildContext context, AsyncSnapshot<List<Datum>> snapshot) {
return Padding(
padding: EdgeInsets.only(top: 16.0),
child: NotificationListener<ScrollEndNotification>(
onNotification: (scrollEnd) {
if (scrollEnd.metrics.pixels >=
scrollEnd.metrics.maxScrollExtent * .45) {
_provider.nextPage();
}
return true;
},
child: SingleChildScrollView(
child: Column(
children: [
GridView.builder(
shrinkWrap: true,
physics: BouncingScrollPhysics(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
),
itemCount:
_provider.dataLentgh > 16 ? _provider.dataLentgh : 18,
itemBuilder: (ctx, idx) {
Datum? _data = snapshot.data == null
? …Run Code Online (Sandbox Code Playgroud) 我一直在寻找grpc-go健康检查的文档和代码示例。
寻找问题
没有明确的答案可以在我的程序中重复使用来实施健康检查。