Skip to content

Commit 00e92e2

Browse files
committed
feature: gritty: client: get rid of mock-require
1 parent 2ed7a62 commit 00e92e2

2 files changed

Lines changed: 58 additions & 30 deletions

File tree

client/gritty.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
require('@xterm/xterm/css/xterm.css');
44

55
const {FitAddon} = require('@xterm/addon-fit');
6-
const {WebglAddon} = require('@xterm/addon-webgl');
6+
const {WebglAddon: _WebglAddon} = require('@xterm/addon-webgl');
77
const currify = require('currify');
88
const {tryCatch} = require('try-catch');
99

1010
const wrap = require('wraptile');
1111

1212
const {io} = require('socket.io-client');
13-
const {Terminal} = require('@xterm/xterm');
13+
const {Terminal: _Terminal} = require('@xterm/xterm');
1414

1515
const getEl = require('./get-el');
1616
const getHost = require('./get-host');
@@ -47,10 +47,16 @@ function gritty(element, options = {}) {
4747
command,
4848
autoRestart,
4949
cwd,
50+
connect,
51+
Terminal = _Terminal,
52+
WebglAddon = _WebglAddon,
5053
} = options;
5154

5255
const env = getEnv(options.env || {});
53-
const socket = connect(prefix, socketPath);
56+
57+
const socket = doConnect(prefix, socketPath, {
58+
connect,
59+
});
5460

5561
return createTerminal(el, {
5662
env,
@@ -59,12 +65,26 @@ function gritty(element, options = {}) {
5965
autoRestart,
6066
socket,
6167
fontFamily,
68+
Terminal,
69+
WebglAddon,
6270
});
6371
}
6472

65-
function createTerminal(terminalContainer, {env, cwd, command, autoRestart, socket, fontFamily}) {
73+
function createTerminal(terminalContainer, overrides) {
74+
const {
75+
env,
76+
cwd,
77+
command,
78+
autoRestart,
79+
socket,
80+
fontFamily,
81+
Terminal,
82+
WebglAddon,
83+
} = overrides;
84+
6685
const fitAddon = new FitAddon();
6786
const webglAddon = new WebglAddon();
87+
6888
const terminal = new Terminal({
6989
scrollback: 1000,
7090
tabStopWidth: 4,
@@ -145,12 +165,13 @@ function _onWindowResize(fitAddon) {
145165
tryCatch(fit);
146166
}
147167

148-
function connect(prefix, socketPath) {
168+
function doConnect(prefix, socketPath, overrides = {}) {
169+
const {connect = io.connect} = overrides;
149170
const href = getHost();
150171
const FIVE_SECONDS = 5000;
151172

152173
const path = `${socketPath}/socket.io`;
153-
const socket = io.connect(href + prefix, {
174+
const socket = connect(href + prefix, {
154175
'max reconnection attempts': 2 ** 32,
155176
'reconnection limit': FIVE_SECONDS,
156177
path,

test/client/gritty.js

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
'use strict';
22

3-
const noop = () => {};
43
const {test, stub} = require('supertape');
54

65
require('css-modules-require-hook/preset');
76

87
globalThis.document = {};
98
globalThis.self = {};
10-
globalThis.addEventListener= stub();
11-
12-
const mock = require('mock-require');
13-
14-
const connect = stub().returns({
15-
on: stub(),
16-
});
9+
globalThis.addEventListener = stub();
1710

1811
const open = stub();
1912
const focus = stub();
@@ -31,19 +24,10 @@ const Terminal = stub().returns({
3124

3225
Terminal.applyAddon = stub();
3326

34-
mock('socket.io-client/dist/socket.io', {
35-
connect,
36-
});
37-
38-
mock('@xterm/xterm', {
39-
Terminal,
40-
});
41-
42-
mock('@xterm/xterm-addong-webl', {
43-
WebglAddon: noop,
44-
});
45-
4627
const gritty = require('../../client/gritty');
28+
const connect = stub().returns({
29+
on: stub(),
30+
});
4731

4832
const {
4933
_onConnect,
@@ -58,14 +42,19 @@ const {
5842
test('gritty: Terminal: new', (t) => {
5943
before();
6044

61-
gritty();
45+
gritty(null, {
46+
connect,
47+
Terminal,
48+
WebglAddon: stub(),
49+
});
6250
t.ok(Terminal.calledWithNew(), 'should have been called with new');
6351
after();
6452

6553
t.end();
6654
});
6755

6856
test('gritty: Terminal: args', (t) => {
57+
const WebglAddon = stub();
6958
before();
7059

7160
const args = {
@@ -75,7 +64,11 @@ test('gritty: Terminal: args', (t) => {
7564
allowProposedApi: true,
7665
};
7766

78-
gritty();
67+
gritty(null, {
68+
connect,
69+
Terminal,
70+
WebglAddon,
71+
});
7972

8073
t.calledWith(Terminal, [args], 'should have been called with args');
8174

@@ -88,6 +81,7 @@ test('gritty: Terminal: args: fontFamily', (t) => {
8881

8982
const fontFamily = 'Droid Sans Mono';
9083
const el = {};
84+
const WebglAddon = stub();
9185

9286
const args = {
9387
scrollback: 1000,
@@ -98,6 +92,9 @@ test('gritty: Terminal: args: fontFamily', (t) => {
9892

9993
gritty(el, {
10094
fontFamily,
95+
connect,
96+
Terminal,
97+
WebglAddon,
10198
});
10299

103100
t.calledWith(Terminal, [args], 'should have been called with args');
@@ -108,10 +105,15 @@ test('gritty: Terminal: args: fontFamily', (t) => {
108105

109106
test('gritty: Terminal: open', (t) => {
110107
const el = {};
108+
const WebglAddon = stub();
111109

112110
before();
113111

114-
gritty(el);
112+
gritty(el, {
113+
connect,
114+
Terminal,
115+
WebglAddon,
116+
});
115117
t.calledWith(open, [el], 'should have been called');
116118
after();
117119

@@ -120,10 +122,15 @@ test('gritty: Terminal: open', (t) => {
120122

121123
test('gritty: Terminal: focus', (t) => {
122124
const el = {};
125+
const WebglAddon = stub();
123126

124127
before();
125128

126-
gritty(el);
129+
gritty(el, {
130+
connect,
131+
Terminal,
132+
WebglAddon,
133+
});
127134
t.calledWithNoArgs(focus, 'should have been called');
128135
after();
129136

0 commit comments

Comments
 (0)