Skip to content

Commit

Permalink
test: 修改 talk 插件测试
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-light committed Mar 22, 2024
1 parent fb6231b commit fee8ad9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 36 deletions.
6 changes: 3 additions & 3 deletions lib/plugins/message/talk.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ export class FTalkMessagePlugin extends FBasePlugin {
const response = await fetch(api.url, api.init);
if (!response.ok) continue;
this._message.text = await api.handle(response);
// 获取文本后才设置 condition 返回true
this._message.condition = this.condition.bind(this._message, this._live2d.tips);
this._live2d.tips.addMessage(this._message);
break;
}
catch (_) {}
}
// 获取文本后才设置 condition 返回true
this._message.condition = this.condition.bind(this._message, this._live2d.tips);
this._live2d.tips.addMessage(this._message);
}
catch (_) {}
}
Expand Down
9 changes: 4 additions & 5 deletions test/jest/controller.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ describe('ULive2dController 单元测试', () => {

describe('UStageController 单元测试', () => {
const event = jest.spyOn(UBaseController.prototype, 'event', 'get');
const live2d = jest.spyOn(UBaseController.prototype, 'live2d', 'get');
const live2dData = jest.spyOn(UBaseController.prototype, 'live2dData', 'get');
const app = jest.spyOn(UBaseController.prototype, 'app', 'get');
const ref = jest.spyOn(UBaseController.prototype, 'ref', 'get');
Expand All @@ -86,18 +85,16 @@ describe('UStageController 单元测试', () => {
event.mockClear();
// 加入数据
expect(() => stage = new UStageController(wlLive2d)).not.toThrow();
expect(event).toHaveBeenCalledTimes(1);
expect(event).toHaveBeenCalledTimes(2);
// 替换 stage
wlLive2d.stage.destroy();
wlLive2d._stage = stage;
// 检查有效性
expect(stage).toBeObject();
});
test('测试 init 函数', () => {
expect(initFun).not.toHaveBeenCalled();
wlLive2d.event.emit(EEvent.init);
expect(() => stage.init()).not.toThrow();
expect(initFun).toHaveBeenCalled();
expect(live2d).toHaveBeenCalled();
expect(live2dData).toHaveBeenCalled();
expect(ref).toHaveBeenCalled();
wlLive2d.data.fixed = false;
Expand Down Expand Up @@ -134,10 +131,12 @@ describe('UStageController 单元测试', () => {
// 使用假的定时器
//jest.useFakeTimers({ advanceTimers: true });
jest.useRealTimers();
const div = document.createElement('div');
await expect(cat(stage['_fade']())).resolves.pass('通过');
await expect(cat(stage['_fade'](null))).resolves.pass('通过');
await expect(cat(stage['_fade'](stage.menus, 'fadeOut'))).resolves.pass('通过');
await expect(cat(stage['_fade'](stage.menus, 'fadeOut', 'fadeOut'))).resolves.pass('通过');
await expect(cat(stage.fadeOut(div))).resolves.pass('通过');
await expect(cat(stage.fadeOut())).resolves.pass('通过');
await expect(cat(stage.fadeIn())).resolves.pass('通过');
await expect(cat(stage.fadeOut(stage.menus))).resolves.pass('通过');
Expand Down
78 changes: 50 additions & 28 deletions test/jest/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,7 @@ import val from './__mocks__/variable.js';

global.PIXI = jest.mocked(val.pixiVal);
global.ILive2DModel = jest.mocked(val.live2DModelVal);
global.fetch = jest.fn(async (input, init) => {
return {
json() {
return Promise.reject({ hitokoto: {} });
},
text() {
if (/tenapi/.test(input)) {
return input;
}
return Promise.reject({});
},
blob() {
return input;
}
};
});

window.open = jest.fn();
window.visualViewport = Object.assign({ width: 20, height: 20 });
jest.spyOn(window, 'screen', 'get').mockImplementation(() => window.visualViewport);
Expand Down Expand Up @@ -63,6 +48,11 @@ describe('plugins 测试', () => {
// https://www.coder.work/article/7760377
global.URL.createObjectURL = jest.fn();
HTMLAnchorElement.prototype.click = jest.fn();
global.fetch = jest.fn().mockImplementation(async (input) => {
return {
blob: () => input
};
});
const baseEnable = jest.spyOn(FCapturePlugin.prototype, 'isEnable', null).mockImplementation(() => enable);
jest.useFakeTimers();
let plugin = new FCapturePlugin;
Expand Down Expand Up @@ -168,8 +158,7 @@ describe('plugins 测试', () => {
test.each([
{ classes: FNullMessagePlugin, name: 'FNullMessagePlugin' },
{ classes: FHourMessagePlugin, name: 'FHourMessagePlugin' },
{ classes: FSeasonsMessagePlugin, name: 'FSeasonsMessagePlugin' },
{ classes: FTalkMessagePlugin, name: 'FSentenceMessagePlugin' }
{ classes: FSeasonsMessagePlugin, name: 'FSeasonsMessagePlugin' }
])('测试 $name', async ({ classes }) => {
const baseEnable = jest.spyOn(classes.prototype, 'isEnable', null).mockImplementation(() => enable);
let plugin = new classes;
Expand All @@ -182,16 +171,6 @@ describe('plugins 测试', () => {
if (classes === FNullMessagePlugin) {
expect(message.condition()).toBeTrue();
}
else if (classes === FTalkMessagePlugin) {
let message = plugin._message;
// 去掉定时器
clearInterval(plugin._handler);
expect(message.condition()).toBeFalse();
jest.runAllTimers();
await expect(plugin.getTalkValue()).resolves.pass('通过');
expect(message.condition()).toBeTrue();
expect(message.condition()).toBeFalse();
}
else {
const now = new Date();
const hour = now.getHours();
Expand All @@ -214,6 +193,49 @@ describe('plugins 测试', () => {
expect(() => live2d.uninstallPlugin(plugin)).not.toThrow();
});

test('测试 FTalkMessagePlugin', async () => {
let okStr = 'v1.hitokoto.cn';
const baseEnable = jest.spyOn(FTalkMessagePlugin.prototype, 'isEnable', null).mockImplementation(() => enable);
global.fetch = jest.fn().mockImplementation(/**@param{string} input*/async (input) => {
return {
ok: input.search(okStr) >= 0,
json() {
return Promise.reject({ hitokoto: {} });
},
text() {
if (/tenapi/.test(input)) {
return input;
}
return Promise.reject({});
},
blob() {
return input;
}
};
});
let plugin = new FTalkMessagePlugin;
live2d.tips.data.talkApis = [{ url: '', handle: async () => '' }];
expect(() => testPlugin(plugin, baseEnable)).not.toThrow();
live2d.tips.data.talkApis = [];
expect(() => live2d.installPlugin(plugin)).not.toThrow();
let message = plugin._message;
// 去掉定时器
clearInterval(plugin._handler);
expect(message.condition()).toBeFalse();
jest.runAllTimers();
okStr = 'v1.hitokoto.cn';
await expect(plugin.getTalkValue()).resolves.pass('通过');
okStr = 'v.api.aa1.cn';
await expect(plugin.getTalkValue()).resolves.pass('通过');
okStr = 'tenapi.cn';
await expect(plugin.getTalkValue()).resolves.pass('通过');
expect(message.condition()).toBeTrue();
expect(message.condition()).toBeFalse();
baseEnable.mockRestore();
expect(plugin.isEnable()).toBeTrue();
expect(() => live2d.uninstallPlugin(plugin)).not.toThrow();
});

test('测试 FMotionMessagePlugin', async () => {
const baseEnable = jest.spyOn(FMotionMessagePlugin.prototype, 'isEnable', null).mockImplementation(() => enable);
let plugin = new FMotionMessagePlugin;
Expand Down

0 comments on commit fee8ad9

Please sign in to comment.