chai-kefir
用於斷言 Kefir Observables 的 Chai 外掛。
如何使用
使用 npm 安裝
npm i --save-dev chai-kefir
在測試檔案的頂端,匯入 chai-kefir
和 kefir
並註冊 Kefir
import Kefir from 'kefir';
import { use } from 'chai';
import chaiKefir from 'chai-kefir';
如果您沒有使用 ESModules,請務必抓取 default
屬性
const Kefir = require('kefir');
const { use } = require('chai');
const chaiKefir = require('chai-kefir').default;
在測試檔案的頂端,使用匯出的工廠函式建立外掛,並向 chai
註冊
const { plugin, activate, send, stream, prop, pool } = chaiKefir(Kefir);
use(plugin);
所有匯出的函式都能讓您與 Kefir Observables 互動,而無需將它們直接連接到真實或模擬的來源。
API
工廠: (Kefir) => PluginHelpers
預設匯出是一個工廠函式,它接受應用程式的 Kefir 實例,並傳回外掛輔助物件。這些輔助物件的說明如下。
PluginHelpers
plugin: (chai, utils) => void
plugin
函式會向 chai
註冊 chai-kefir
的斷言。此函式應傳遞給 chai
的 use
函式。
activate: (obs: Kefir.Observable) => void
activate
是一個簡單的輔助函式,可開啟串流。
deactivate: (obs: Kefir.Observable) => void
deactivate
是一個簡單的輔助函式,可關閉串流。它可以關閉透過 activate
啟動的串流。透過其他方式(直接呼叫 on{Value|Error|End|Any}
、使用 observe
等)啟動的串流需要透過其互補機制停用。
send: (obs: Kefir.Observable, values: Array<Event<T>>) => obs
send
是一個輔助函式,用於將值發射到給定的 observable 中。請注意,第二個參數是要從 observable 發射的值陣列。Event
是由 value
、error
和 end
函式產生。對於這三個函式,不需要選用的 options
物件。
value: (value, options: ?{ current }) => Event<Value>
error: (error, options: ?{ current }) => Event<Error>
end: (options: ?{ current }) => Event<End>
value
和 error
會接受一個值或錯誤以及一個選用的 options
物件,並傳回一個 Event
物件,該物件可以傳遞給 send
、emit
或 emitInTime
。end
不會接受此值,因為 Kefir 中的 end
事件不會傳送值。
當傳遞給 send
時,會忽略 options
物件。options
由 emit
和 emitInTime
(如下所述)使用,以決定是否將事件視為 Kefir.Property
的目前事件、錯誤或結束。
stream: () => Kefir.Stream
prop: () => Kefir.Property
pool: () => Kefir.Pool
stream
、prop
和 pool
是用於建立空的串流、屬性和集區的輔助函式。這些函式可以用作模擬來源,以傳送值。它們沒有其他行為。
斷言
observable
斷言預期的值是否為 Kefir.Observable
。對於其他斷言,我們建議從 observable
鏈接。下方的 property
需要它;其餘的應該為了保持一致性。
expect(obs).to.be.an.observable();
property
斷言預期的值是否為 Kefir.Property
。必須與 observable
鏈接。
expect(obs).to.be.an.observable.property();
stream
斷言預期的值是否為 Kefir.Stream
。
expect(obs).to.be.an.observable.stream();
pool
斷言預期的值是否為 Kefir.Pool
。
expect(obs).to.be.an.observable.pool();
active
斷言預期的值是否為作用中的 observable。
expect(obs).to.be.an.active.observable();
emit
斷言所提供的 observable 是否同步發射預期的值。emit
接受要比對的值陣列,並預期它們與正確順序的值深度相等。
接受在啟動 observable 後呼叫的選用回呼。這是因為在將值傳遞給 chai
之前發射到 observable 的值將不會發射到斷言中,除非它是屬性。
expect(obs).to.emit([value(1), error(new Error('whoops!')), end()], () => {
send(obs, [value(1), error(new Error('whoops!')), end()]);
});
如果 obs
是具有目前值的 Kefir.Property
,則預期的值應該取得帶有 current: true
的選項物件。請注意,根據屬性的運作方式,只有最後一個值是目前的。
send(obs, [value(1)]);
send(obs, [value(2)]);
expect(obs).to.emit([value(2, { current: true }), end()], () => {
send(obs, [end()]);
});
這些規則也適用於 emitInTime
。
emitInTime
斷言所提供的程式碼是否隨時間正確發射值。在幕後使用 lolex
接管 JavaScript 的計時器,讓您可以根據發射值的時間進行斷言。預期的值應該是元組陣列,其中第一個值是時間,第二個值是發射的值。
const expected = [
[0, value(1)],
[10, error(new Error('whoops!'))],
[20, end()]
]
接受一個回呼,該回呼會同時傳遞一個簡單的 tick
函式以及完整的 lolex
clock
。tick
會將內部計時器提前指定的毫秒數。clock
的文件在此。
expect(obs).to.emit(expected, (tick, clock) => {
send(obs, [value(1)]);
tick(10);
send(obs, [error(new Error('whoops!'))]);
tick(10);
send(obs, [end()]);
});
emitInTime
也會在回呼後接受一個選用的組態物件。該物件會採用下列選項
-
reverseSimultaneous: bool
:指出是否應以相反順序呼叫在同一時間排定的回呼。這是一種進階使用案例,用於檢查您的實作是否能處理常見的瀏覽器錯誤。請參閱此問題以取得更多資訊。Kefir 的內建方法會正確處理此問題,因此除非您在實作中使用計時器,否則這大多是不必要的。