Add initial test structure and some tests for Window

This commit is contained in:
Gustav Hansen 2019-02-17 11:39:10 -05:00
parent 0e00f43356
commit f3ff3d4e22
6 changed files with 10598 additions and 1112 deletions

View file

@ -1,7 +1,31 @@
{
"presets": [
"env",
"stage-0",
"react"
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
// Stage 0
"@babel/plugin-proposal-function-bind",
// Stage 1
"@babel/plugin-proposal-export-default-from",
"@babel/plugin-proposal-logical-assignment-operators",
["@babel/plugin-proposal-optional-chaining", { "loose": false }],
["@babel/plugin-proposal-pipeline-operator", { "proposal": "minimal" }],
["@babel/plugin-proposal-nullish-coalescing-operator", { "loose": false }],
"@babel/plugin-proposal-do-expressions",
// Stage 2
["@babel/plugin-proposal-decorators", { "legacy": true }],
"@babel/plugin-proposal-function-sent",
"@babel/plugin-proposal-export-namespace-from",
"@babel/plugin-proposal-numeric-separator",
"@babel/plugin-proposal-throw-expressions",
// Stage 3
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-syntax-import-meta",
["@babel/plugin-proposal-class-properties", { "loose": false }],
"@babel/plugin-proposal-json-strings"
]
}

307
__mocks__/libui-node.js Normal file
View file

@ -0,0 +1,307 @@
// From Vuido tests, with modifications
const libui = {};
libui.classFuncs = {
UiWindow: {
setChild: jest.fn().mockImplementation(child => {}),
show: jest.fn(),
close: jest.fn(),
onClosing: jest.fn().mockImplementation(handler => {
handler();
}),
onContentSizeChanged: jest.fn().mockImplementation(handler => {
handler();
}),
fullscreen: false,
margined: false,
borderless: false,
contentSize: {
h: 500,
w: 500,
},
},
UiControl: {
visible: true,
enabled: true,
destroy: jest.fn(),
},
UiLabel: {
text: '',
setParent: jest.fn(),
},
};
libui.UiWindow = jest.fn().mockImplementation((title, width, height, menu) => {
return libui.classFuncs.UiWindow;
});
libui.UiControl = jest.fn().mockImplementation(() => {
return libui.classFuncs.UiControl;
});
libui.UiBox = class extends libui.UiControl {
constructor() {
super();
this.padded = false;
this.children = [];
}
append(control, stretchy) {
this.children.push(control);
}
deleteAt(index) {
if (index < 0 || index >= this.children.length)
throw new RangeError('Invalid control index');
this.children.splice(index, 1);
}
};
libui.UiHorizontalBox = class extends libui.UiBox {};
libui.UiVerticalBox = class extends libui.UiBox {};
libui.UiForm = class extends libui.UiControl {
constructor() {
super();
this.padded = false;
this.children = [];
}
append(label, control, stretchy) {
this.children.push(control);
}
deleteAt(index) {
if (index < 0 || index >= this.children.length)
throw new RangeError('Invalid control index');
this.children.splice(index, 1);
}
};
libui.UiGroup = class extends libui.UiControl {
constructor() {
super();
this.title = '';
this.margined = false;
}
setChild(control) {}
};
libui.UiTab = class extends libui.UiControl {
constructor() {
super();
this.children = [];
}
append(label, control) {
this.children.push(control);
}
deleteAt(index) {
if (index < 0 || index >= this.children.length)
throw new RangeError('Invalid control index');
this.children.splice(index, 1);
}
numPages() {
return this.children.length;
}
setMargined(index, margined) {}
};
libui.UiButton = class extends libui.UiControl {
constructor() {
super();
this.text = '';
}
onClicked(handler) {}
};
libui.UiLabel = jest.fn().mockImplementation(() => {
return { ...libui.classFuncs.UiControl, ...libui.classFuncs.UiLabel };
});
libui.UiEntryBase = class extends libui.UiControl {
constructor() {
super();
this.text = '';
this.readOnly = false;
}
onChanged(handler) {}
};
libui.UiEntry = class extends libui.UiEntryBase {};
libui.UiPasswordEntry = class extends libui.UiEntryBase {};
libui.UiSearchEntry = class extends libui.UiEntryBase {};
libui.UiCheckbox = class extends libui.UiControl {
constructor() {
super();
this.text = '';
this.checked = false;
}
onToggled(handler) {}
};
libui.UiColorButton = class extends libui.UiControl {
constructor() {
super();
this.color = null;
}
onChanged(handler) {}
};
libui.UiEditableCombobox = class extends libui.UiControl {
constructor() {
super();
this.items = [];
this.text = '';
}
append(item) {
this.items.push(item);
}
onChanged(handler) {}
};
libui.UiCombobox = class extends libui.UiControl {
constructor() {
super();
this.items = [];
this.selected = 0;
}
append(item) {
this.items.push(item);
}
onSelected(handler) {}
};
libui.UiProgressBar = class extends libui.UiControl {
constructor() {
super();
this.value = 0;
}
};
libui.UiRadioButtons = class extends libui.UiControl {
constructor() {
super();
this.items = [];
this.selected = 0;
}
append(item) {
this.items.push(item);
}
onSelected(handler) {}
};
libui.UiVerticalSeparator = class extends libui.UiControl {};
libui.UiHorizontalSeparator = class extends libui.UiControl {};
libui.UiSlider = class extends libui.UiControl {
constructor(min, max) {
super();
this.min = min;
this.max = max;
this.value = 0;
}
onChanged(handler) {}
};
libui.UiSpinbox = class extends libui.UiControl {
constructor(min, max) {
super();
this.min = min;
this.max = max;
this.value = 0;
}
onChanged(handler) {}
};
libui.UiMultilineEntry = class extends libui.UiControl {
constructor() {
super();
this.text = '';
this.readOnly = false;
}
onChanged(handler) {}
};
libui.UiArea = class extends libui.UiControl {
constructor(
drawCb,
mouseEventCb,
mouseCrossedCb,
dragBrokenCb,
keyEventCb,
width,
heigth
) {
super();
}
setSize(width, height) {}
queueRedrawAll() {}
};
libui.Color = class {
constructor(r, g, b, a) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
};
libui.Size = class {
constructor(width, height) {
this.w = width;
this.h = height;
}
};
libui.DrawStrokeParams = class {
constructor() {
this.thickness = 0;
}
};
libui.startLoop = jest.fn();
libui.stopLoop = jest.fn();
libui.UiDialogs = {
msgBox(parent, title, description) {},
msgBoxError(parent, title, description) {},
openFile(parent) {},
saveFile(parent) {},
};
libui.Ui = {
onShouldQuit(func) {},
};
module.exports = libui;
module.exports.defa;

297
__mocks__/libui_bare.js Normal file
View file

@ -0,0 +1,297 @@
// From Vuido tests, with modifications
const libui = {};
libui.UiWindow = class {
constructor(title, width, height, menu) {
this.title = title;
this.contentSize = new libui.Size(width, height);
this.menu = menu;
this.margined = false;
this.fullscreen = false;
this.borderless = false;
}
setChild(child) {}
show() {}
close() {}
onClosing(handler) {}
onContentSizeChanged(handler) {}
};
libui.UiControl = class {
constructor() {
this.visible = true;
this.enabled = true;
}
destroy() {}
};
libui.UiBox = class extends libui.UiControl {
constructor() {
super();
this.padded = false;
this.children = [];
}
append(control, stretchy) {
this.children.push(control);
}
deleteAt(index) {
if (index < 0 || index >= this.children.length)
throw new RangeError('Invalid control index');
this.children.splice(index, 1);
}
};
libui.UiHorizontalBox = class extends libui.UiBox {};
libui.UiVerticalBox = class extends libui.UiBox {};
libui.UiForm = class extends libui.UiControl {
constructor() {
super();
this.padded = false;
this.children = [];
}
append(label, control, stretchy) {
this.children.push(control);
}
deleteAt(index) {
if (index < 0 || index >= this.children.length)
throw new RangeError('Invalid control index');
this.children.splice(index, 1);
}
};
libui.UiGroup = class extends libui.UiControl {
constructor() {
super();
this.title = '';
this.margined = false;
}
setChild(control) {}
};
libui.UiTab = class extends libui.UiControl {
constructor() {
super();
this.children = [];
}
append(label, control) {
this.children.push(control);
}
deleteAt(index) {
if (index < 0 || index >= this.children.length)
throw new RangeError('Invalid control index');
this.children.splice(index, 1);
}
numPages() {
return this.children.length;
}
setMargined(index, margined) {}
};
libui.UiButton = class extends libui.UiControl {
constructor() {
super();
this.text = '';
}
onClicked(handler) {}
};
libui.UiLabel = class extends libui.UiControl {
constructor() {
super();
this.text = '';
}
};
libui.UiEntryBase = class extends libui.UiControl {
constructor() {
super();
this.text = '';
this.readOnly = false;
}
onChanged(handler) {}
};
libui.UiEntry = class extends libui.UiEntryBase {};
libui.UiPasswordEntry = class extends libui.UiEntryBase {};
libui.UiSearchEntry = class extends libui.UiEntryBase {};
libui.UiCheckbox = class extends libui.UiControl {
constructor() {
super();
this.text = '';
this.checked = false;
}
onToggled(handler) {}
};
libui.UiColorButton = class extends libui.UiControl {
constructor() {
super();
this.color = null;
}
onChanged(handler) {}
};
libui.UiEditableCombobox = class extends libui.UiControl {
constructor() {
super();
this.items = [];
this.text = '';
}
append(item) {
this.items.push(item);
}
onChanged(handler) {}
};
libui.UiCombobox = class extends libui.UiControl {
constructor() {
super();
this.items = [];
this.selected = 0;
}
append(item) {
this.items.push(item);
}
onSelected(handler) {}
};
libui.UiProgressBar = class extends libui.UiControl {
constructor() {
super();
this.value = 0;
}
};
libui.UiRadioButtons = class extends libui.UiControl {
constructor() {
super();
this.items = [];
this.selected = 0;
}
append(item) {
this.items.push(item);
}
onSelected(handler) {}
};
libui.UiVerticalSeparator = class extends libui.UiControl {};
libui.UiHorizontalSeparator = class extends libui.UiControl {};
libui.UiSlider = class extends libui.UiControl {
constructor(min, max) {
super();
this.min = min;
this.max = max;
this.value = 0;
}
onChanged(handler) {}
};
libui.UiSpinbox = class extends libui.UiControl {
constructor(min, max) {
super();
this.min = min;
this.max = max;
this.value = 0;
}
onChanged(handler) {}
};
libui.UiMultilineEntry = class extends libui.UiControl {
constructor() {
super();
this.text = '';
this.readOnly = false;
}
onChanged(handler) {}
};
libui.UiArea = class extends libui.UiControl {
constructor(
drawCb,
mouseEventCb,
mouseCrossedCb,
dragBrokenCb,
keyEventCb,
width,
heigth
) {
super();
}
setSize(width, height) {}
queueRedrawAll() {}
};
libui.Color = class {
constructor(r, g, b, a) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
};
libui.Size = class {
constructor(width, height) {
this.w = width;
this.h = height;
}
};
libui.DrawStrokeParams = class {
constructor() {
this.thickness = 0;
}
};
libui.startLoop = function() {};
libui.stopLoop = function() {};
libui.UiDialogs = {
msgBox(parent, title, description) {},
msgBoxError(parent, title, description) {},
openFile(parent) {},
saveFile(parent) {},
};
module.exports = libui;

10946
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -15,7 +15,8 @@
"prettier --single-quote --trailing-comma es5 --write 'src/**/*.js' 'examples/*.js' './**/*.md' Demo.js",
"build": "babel src -d bin",
"pub": "babel src -d bin && npm publish",
"precommit": "lint-staged"
"precommit": "lint-staged",
"test": "jest"
},
"repository": {
"type": "git",
@ -37,11 +38,31 @@
"svg-path-parser": "^1.1.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/node": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.0.0",
"@babel/plugin-proposal-decorators": "^7.0.0",
"@babel/plugin-proposal-do-expressions": "^7.0.0",
"@babel/plugin-proposal-export-default-from": "^7.0.0",
"@babel/plugin-proposal-export-namespace-from": "^7.0.0",
"@babel/plugin-proposal-function-bind": "^7.0.0",
"@babel/plugin-proposal-function-sent": "^7.0.0",
"@babel/plugin-proposal-json-strings": "^7.0.0",
"@babel/plugin-proposal-logical-assignment-operators": "^7.0.0",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.0.0",
"@babel/plugin-proposal-numeric-separator": "^7.0.0",
"@babel/plugin-proposal-optional-chaining": "^7.0.0",
"@babel/plugin-proposal-pipeline-operator": "^7.0.0",
"@babel/plugin-proposal-throw-expressions": "^7.0.0",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/plugin-syntax-import-meta": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^23.4.2",
"husky": "^0.14.3",
"jest": "^24.1.0",
"lint-staged": "^7.1.0",
"prettier": "^1.12.1",
"react-devtools": "^3.4.3"

99
test/Window.test.js Normal file
View file

@ -0,0 +1,99 @@
import React, { Component } from 'react';
import { render, App, Window, Text } from '../src/';
jest.mock('libui-node');
const libui = require('libui-node');
describe('Window and loop', () => {
beforeEach(() => {
jest.clearAllMocks();
});
test('All defaults', () => {
class Test extends Component {
render() {
return (
<App>
<Window />
</App>
);
}
}
render(<Test />);
expect(libui.startLoop).toHaveBeenCalled();
expect(libui.stopLoop).toHaveBeenCalled();
expect(libui.UiWindow).toHaveBeenCalledWith('', 500, 500, true);
expect(libui.classFuncs.UiWindow.show).toHaveBeenCalled();
expect(libui.classFuncs.UiWindow.setChild).not.toHaveBeenCalled();
}),
test('All options and close', () => {
const onClose = jest.fn();
const onContentSizeChange = jest.fn();
class Test extends Component {
render() {
return (
<App>
<Window
title="Test title"
size={{ w: 200, h: 600 }}
menuBar={false}
margined={true}
fullscreen={true}
borderless={true}
lastWindow={false}
onClose={onClose}
onContentSizeChange={onContentSizeChange}
/>
</App>
);
}
}
render(<Test />);
expect(libui.UiWindow).toHaveBeenCalledWith(
'Test title',
200,
600,
false
);
expect(libui.classFuncs.UiWindow.fullscreen).toBe(true);
expect(libui.classFuncs.UiWindow.margined).toBe(true);
expect(libui.classFuncs.UiWindow.borderless).toBe(true);
expect(onClose).toHaveBeenCalled();
expect(libui.stopLoop).not.toHaveBeenCalled();
expect(libui.classFuncs.UiWindow.close).toHaveBeenCalled();
expect(onContentSizeChange).toHaveBeenCalled();
}),
test('Multiple children', () => {
class Test extends Component {
render() {
return (
<App>
<Window>
<Text>HI</Text>
<Text>HI2</Text>
</Window>
</App>
);
}
}
expect(() => render(<Test />)).toThrowError();
}),
test('One child', () => {
class Test extends Component {
render() {
return (
<App>
<Window>
<Text>HI</Text>
</Window>
</App>
);
}
}
render(<Test />);
expect(libui.classFuncs.UiWindow.setChild).toHaveBeenCalled();
});
});