javascript - 模拟 Cookie 存储以返回自定义响应对象
问题描述
我在使用cookieStore
.
我怎样才能模拟cookieStore
成为一个有效的对象并让它有
相关的值来测试它?
我希望模拟它,使其cookieStore
在我执行 get 时等于跟随对象。
cookieStore.get('myCookie')
应该返回以下对象。
{
'name' : 'cookiename',
'expires' : '679543860927183',
}
这是函数(如果相关,它是异步的)。
const checkCookie = async () => {
const exists = typeof cookieStore === 'object';
if (!exists) {
// right now cookieStore always doesn't exist in test
// thus always end up here.
return 'store not found';
}
const myCookie = await cookieStore.get('mycookie');
const {expires} = { ...myCookie };
if (expires) {
return 'cookie expired';
}
}
目前测试失败,因为 cookieStore 未定义
it('should return cookie expired text', async () => {
const result = await checkCookie();
expect(result).toBe('cookie expired');
});
注意:我没有导入cookieStore
. 它是一个有效的网络 API。您可以转到 Chrome
浏览器的控制台并键入cookieStore
以查看它是否存在。
因此实际上可以按原样复制上述功能。
文档cookieStore
供参考:https ://developer.mozilla.org/en-US/docs/Web/API/CookieStore
解决方案
推荐阅读
- javascript - 检查是否在 P5.js 中单击了弧
- java - 在 PlaceholderFragment 中收集数据并将其发送到 Activity
- qt5 - 更改 qt.cpp 文件后如何编译 yocto rootfs?
- flutter - 从 _internallinkedhashmap Flutter 中获取值
- performance - 使用 CSV 列表搜索目录中的数千个文件
- ios - UIButton标题标签的不同大小的字符串
- java - Spring-boot + Apache camel + Embedded tomcat 显示收据句柄已在 SQS 删除时过期。在外部tomcat中工作正常
- sql-server - 在类路径资源中定义名称为“jpaVendorAdapter”的 bean 创建错误
- java - 使用 QuerydslPredicateExecutor 进行分组
- c - tracepoint/syscalls/sys_enter 不会触发 bpf_trace_printk