diff --git a/docs/en/building/introduction.md b/docs/en/building/introduction.md index ee619cd53..fca165000 100644 --- a/docs/en/building/introduction.md +++ b/docs/en/building/introduction.md @@ -31,12 +31,12 @@ This build will use the `dist` mode to create an application bundle and output t ## Serving and watching -A web server can be started with the `--serve` flag while running in `dev` or `dist` modes. By default the application is served on port 9999. This can be cahnged with the `--port` flag. Dojo's build tool can also watch the application for changes and automatically rebuild with the `--watch` flag. +A web server can be started with the `--serve` flag while running in `dev` or `dist` modes. By default the application is served on port 9999. This can be changed with the `--port` flag. Dojo's build tool can also watch the application for changes and automatically rebuild with the `--watch` flag. The generated `package.json` includes the `dev` script which uses these flags to serve a built version of the application and automatically rebuild it when files change on disk. ```bash -> dojo build --mode dev --watch file --serve +> dojo build --mode dev --watch --serve ``` The application will also have source maps available. This allows the debugger to map the built JavaScript code back to the original TypeScript code that lives in the `src/` directory. diff --git a/docs/en/building/supplemental.md b/docs/en/building/supplemental.md index 35a53ae4d..6022edc09 100644 --- a/docs/en/building/supplemental.md +++ b/docs/en/building/supplemental.md @@ -315,11 +315,17 @@ Dojo provides a blocks system which can execute code in Node.js as part of the b For example, a Dojo Block module could read a group of markdown files, transform them into VNodes, and make them available to render in the application, all at build time. The result of this Dojo Block module is then cached into the application bundle for use at runtime in the browser. -A Dojo Block module gets used like any other meta in any Dojo widget. As a result there is no extensive configuration or alternative authoring patterns needed. +A Dojo Block module gets used like any middleware or meta in a Dojo widget. For the Dojo build system to be able to identify and run a block module there are three requirements that must be met: + +1. The module must have a `.block` suffix, for example `src/readFile.block.ts`. +1. The Block must only have a single default export +1. Return values from blocks (from a promise resolution or as an immediate return) must be serializable to json + +Other than these requirements there is no configuration or alternative authoring pattern required. For example, a block module could read a text file and return the content to the application. -> src/blocks/read-file.ts +> src/readFile.block.ts ```ts import * as fs from 'fs'; @@ -334,21 +340,20 @@ export default (path: string) => { > src/widgets/MyBlockWidget.tsx ```tsx -import Block from '@dojo/framework/core/meta/Block'; -import WidgetBase from '@dojo/framework/core/WidgetBase'; -import { v } from '@dojo/framework/core/vdom'; +import { create, tsx } from '@dojo/framework/core/vdom'; +import block from '@dojo/framework/core/middleware/block'; -import readFile from '../blocks/read-file'; +import readFile from '../readFile.block'; -export default class MyBlockWidget extends WidgetBase { - protected render() { - const message = this.meta(Block).run(readFile)('../content/hello-dojo-blocks.txt'); - return v('div', [message]); - } -} +const factory = create({ block }); + +export default factory(function MyBlockWidget({ middleware: { block } }) { + const message = block(readFile)('../content/hello-dojo-blocks.txt'); + return
api data); + }); +}); ``` #### Mock `intersection` middleware @@ -159,6 +267,24 @@ describe('MyWidget', () => { }); ``` +#### Mock `node` middleware + +Using `createNodeMock` from `@dojo/framework/testing/mocks/middleware/node` creates a mock for the node middleware. To set the expected return from the node mock, call the created mock node middleware with a `key` and expected DOM node. + +```ts +import createNodeMock from '@dojo/framework/testing/mocks/middleware/node'; + +// create the mock node middleware +const mockNode = createNodeMock(); + +// create a mock DOM node +const domNode = {}; + +// call the mock middleware with a key and the DOM +// to return. +mockNode('key', domNode); +``` + #### Mock `resize` middleware Using `createResizeMock` from `@dojo/framework/testing/mocks/middleware/resize` creates a mock resize middleware. To set the expected return from the resize mock, call the created mock resize middleware with a `key` and expected content rects. @@ -312,6 +438,48 @@ describe('MyWidget', () => { }); ``` +#### Custom middleware mocks + +Not all testing scenarios will be covered by the provided mocks. Custom middleware mocks can also be created. A middleware mock should provide an overloaded interface. The parameterless overload should return the middleware implementation; this is what will be injected into the widget under test. Other overloads are created as needed to provide an interface for the tests. + +As an example, consider the framework's `icache` mock. The mock provides these overloads: + +```ts +function mockCache(): MiddlewareResult
api data); + }); +}); ``` #### Mock `intersection` 中间件 @@ -163,6 +272,23 @@ describe('MyWidget', () => { }); ``` +#### Mock `node` 中间件 + +使用 `@dojo/framework/testing/mocks/middleware/node` 中的 `createNodeMock` 可 mock 一个 node 中间件。要设置从 node mock 中返回的期望值,需要调用创建的 mock node 中间件,并传入 `key` 和期望的 DOM node。 + +```ts +import createNodeMock from '@dojo/framework/testing/mocks/middleware/node'; + +// 创建一个 mock node 的中间件 +const mockNode = createNodeMock(); + +// mock 一个 DOM 节点 +const domNode = {}; + +// 调用 mock 中间件,并传入 key 和将返回的 DOM +mockNode('key', domNode); +``` + #### Mock `resize` 中间件 使用 `@dojo/framework/testing/mocks/middleware/resize` 中的 `createResizeMock` 可 mock 一个 resize 中间件。要设置从 resize mock 中返回的期望值,需要调用创建的 mock resize 中间件,并传入 `key` 和期望的容纳内容的矩形区域。 @@ -316,6 +442,48 @@ describe('MyWidget', () => { }); ``` +#### 自定义模拟的中间件 + +已提供的模拟(mock)并未覆盖所有的测试场景。也可以创建自定义的模拟中间件。模拟中间件应该提供一个可重载的接口。无参的重载应该返回中间件的实现,它将被注入到被测的部件中。根据需要创建其他重载,以便为测试提供接口。 + +例如,考虑框架中的 `icache` 模拟。这个模拟提供了以下重载: + +```ts +function mockCache(): MiddlewareResult
implement this._properties = diffPropertyResults; this._changedPropertyKeys = changedPropertyKeys; } else { - this._initialProperties = false; for (let i = 0; i < propertyNames.length; i++) { const propertyName = propertyNames[i]; if (typeof properties[propertyName] === 'function') { @@ -276,6 +275,7 @@ export class WidgetBase
implement
this._changedPropertyKeys = changedPropertyKeys;
this._properties = { ...properties };
}
+ this._initialProperties = false;
if (this._changedPropertyKeys.length > 0) {
this.invalidate();
diff --git a/src/core/middleware/focus.ts b/src/core/middleware/focus.ts
index dc1f2e930..3b7aaaa85 100644
--- a/src/core/middleware/focus.ts
+++ b/src/core/middleware/focus.ts
@@ -1,35 +1,34 @@
import global from '../../shim/global';
import { create, diffProperty, node, destroy, invalidator } from '../vdom';
import { createICacheMiddleware } from './icache';
-import { cache } from './cache';
import { FocusProperties } from '../mixins/Focus';
interface FocusState {
- previous: number;
current: number;
}
const icache = createICacheMiddleware