我正在尝试测试具有 async 的 React 组件componentDidMount。
承诺本身不需要被嘲笑,它不一定用于访问外部内容,主要只是道具的包装。
但是,为了测试它,我需要使用wrapper.update() 4 次,这对我来说似乎很奇怪。
解决方案在:
一切都不适合我。
这是我的测试的样子(目前有效,但这个解决方案一点也不优雅,而且可扩展性也不强):
import * as React from 'react'
import { shallow, mount } from 'enzyme'
import LargeSelector from './LargeSelector'
describe('<LargeSelector />', async () => {
const componentDidMountSpy = jest.spyOn(LargeSelector.prototype, 'componentDidMount')
describe('search', async () => {
it('should save initial response in cache', async () => {
const wrapper = await shallow(<LargeSelector query={async (search) => ['search:' + search]} />)
// WHY DO …Run Code Online (Sandbox Code Playgroud) 我正试图在我的脚本中解析BBCode.现在,它无缝地工作,直到我尝试缩进BBCode不仅仅是粗体或下划线 - 例如剧透,网址,字体大小等 - 然后它搞砸了.这是我的代码:
function parse_bbcode($text) {
global $db;
$oldtext = $text;
$bbcodes = $db->select('*', 'bbcodes');
foreach ($bbcodes as $bbcode) {
switch ($bbcode->type) {
case 'simple': {
$find = '{content}';
$replace = '${1}';
$text = preg_replace(
'/\['.$bbcode->tag.'\](.+)\[\/'.$bbcode->tag.'\]/i',
str_replace($find, $replace, $bbcode->html),
$text);
break;
}
case 'property':
case 'options': {
$find = array ( '{property}', '{content}' );
$replace = array ( '${1}', '${2}' );
$text = preg_replace(
'/\['.$bbcode->tag.'\=(.[^\"]*)\](.+)\[\/'.$bbcode->tag.'\]/i',
str_replace($find, $replace, $bbcode->html),
$text);
break;
}
}
}
return $text;
}
Run Code Online (Sandbox Code Playgroud)
现在我的猜测是RegEx不喜欢模式中的递归.我怎样才能改进它?示例$ bbcode对象是这样的: …
奇怪的事发生了.我有一个AsyncTask运行,所以我不在UI上获取数据库数据Thread.这是我的任务:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_list);
if (savedInstanceState == null) {
new QueryProductsTask(this).execute();
}
}
public void populateProductList(Cursor products) {
ProductListAdapter productListAdapter =
new ProductListAdapter(this, R.layout.product_list_row, products);
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(productListAdapter);
}
Run Code Online (Sandbox Code Playgroud)
protected Cursor doInBackground(Void... params) {
android.os.Debug.waitForDebugger();
Log.v("BagIt.AsyncTask", "doInBackground running");
// Fetches info from database:
return new Products(activity.getBaseContext()).select(null, null, null, null);
}
protected void onPostExecute(Cursor c) {
android.os.Debug.waitForDebugger();
Log.v("BagIt.AsyncTask", "onPostExecute running");
// Populates ListView through a custom adapter …Run Code Online (Sandbox Code Playgroud) 我试图将'this'传递给回调函数.我知道这样做的"合适"方法是将其设置为变量并使用变量......但这不是很优雅.
var that = this;
chrome.storage.sync.set(this.defaultOptions, function () {
that.loadOptions();
});
Run Code Online (Sandbox Code Playgroud)
我正在尝试别的东西,但我无法弄明白该怎么做.这是我尝试过的:
chrome.storage.sync.set(this.defaultOptions, (function () {
this.loadOptions();
}).call(this));
Run Code Online (Sandbox Code Playgroud)
但它没有成功.我收到这个错误:
Error in response to storage.set: Error: Invocation of form
get(object, undefined) doesn't match definition
get(optional string or array or object keys, function callback)
Run Code Online (Sandbox Code Playgroud)
关于我试过的其他问题有类似的需求但不完全相同.我怎样才能以最好的方式实现我想要的?