mux-fetch
Version:
68 lines (61 loc) • 2.04 kB
text/typescript
const { configFactory, DEFAULT_CONFIG, compose } = require("../../src");
describe("configFactory", () => {
it("base", () => {
const parmas = configFactory({});
expect(parmas.prefix).toEqual(DEFAULT_CONFIG.prefix);
expect(parmas.fetchOption).toEqual(DEFAULT_CONFIG.fetchOption);
expect(parmas.responseHandle).toEqual(DEFAULT_CONFIG.responseHandle);
// interceptor 会最后 compose 生成函数
expect(parmas.interceptor.requestInterceptor).toEqual(
DEFAULT_CONFIG.interceptor.requestInterceptor
);
expect(parmas.interceptor.responseInterceptor).toEqual(
DEFAULT_CONFIG.interceptor.responseInterceptor
);
expect(parmas.interceptor.errorInterceptor).toEqual(
DEFAULT_CONFIG.interceptor.errorInterceptor
);
});
it("combine", () => {
const config = {
prefix: "a",
fetchOption: {
a: 1,
b: 2
},
responseHandle: () => 1,
interceptor: {
requestInterceptor: [a => a + 1],
responseInterceptor: [b => b + 2],
errorInterceptor: [c => c + 2]
}
};
const base = {
prefix: "b",
fetchOption: {
a: 2,
c: 3
},
responseHandle: () => 2,
interceptor: {
requestInterceptor: [a => a + 1],
responseInterceptor: [b => b + 2]
}
};
const parmas = configFactory(config, base);
// prefix 和 responseHandle 采取覆盖的策略
// config 优先级最大 , 其次是 base ,最小的是 default
expect(parmas.prefix).toEqual(config.prefix);
expect(parmas.responseHandle()).toEqual(1);
// fetchOption 采用合并的策略,如果是同名的 key ,后面覆盖前面
expect(parmas.fetchOption).toEqual({
a: 1,
b: 2,
c: 3
});
// interceptor 会全量 compose 生成函数
expect(parmas.interceptor.requestInterceptor(1)).toEqual(3);
expect(parmas.interceptor.responseInterceptor(1)).toEqual(5);
expect(parmas.interceptor.errorInterceptor(1)).toEqual(3);
});
});