@servicetitan/ko-bridge 40.2.1 → 40.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@servicetitan/ko-bridge",
|
|
3
|
-
"version": "40.
|
|
3
|
+
"version": "40.3.0",
|
|
4
4
|
"description": "Collection of components and utils to connect Knockout and React codebase",
|
|
5
5
|
"homepage": "https://docs.st.dev/docs/frontend/uikit/ko-bridge",
|
|
6
6
|
"repository": {
|
|
@@ -13,7 +13,11 @@
|
|
|
13
13
|
"sideEffects": false,
|
|
14
14
|
"files": [
|
|
15
15
|
"dist",
|
|
16
|
-
"src"
|
|
16
|
+
"src",
|
|
17
|
+
"!**/__tests__",
|
|
18
|
+
"!**/__mocks__",
|
|
19
|
+
"!**/__snapshots__",
|
|
20
|
+
"!**/*.test.*"
|
|
17
21
|
],
|
|
18
22
|
"devDependencies": {
|
|
19
23
|
"@testing-library/dom": "^10.4.2",
|
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
import '@testing-library/jest-dom';
|
|
2
|
-
import { act, screen, waitFor } from '@testing-library/react';
|
|
3
|
-
import ko from 'knockout';
|
|
4
|
-
import { ComponentType } from 'react';
|
|
5
|
-
import ReactDOM from 'react-dom';
|
|
6
|
-
import { koBindingHandlers } from '../ko-binding-handlers';
|
|
7
|
-
|
|
8
|
-
describe('[ko-bridge] koBindingHandlers', () => {
|
|
9
|
-
let el: HTMLElement;
|
|
10
|
-
let mockAllBindings: any;
|
|
11
|
-
let mockBindingContext: any;
|
|
12
|
-
let valueAccessor: () => ComponentType;
|
|
13
|
-
let viewModel: Record<string, any>;
|
|
14
|
-
|
|
15
|
-
interface MockButtonProps {
|
|
16
|
-
title?: string;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function MockButton({ title }: Readonly<MockButtonProps>) {
|
|
20
|
-
return <button>{title ?? 'MockButton'}</button>;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function itRendersComponent(subject: Function) {
|
|
24
|
-
test('renders Component', async () => {
|
|
25
|
-
subject();
|
|
26
|
-
|
|
27
|
-
await waitFor(() => {
|
|
28
|
-
expect(screen.getByRole('button', { name: 'MockButton' })).toBeInTheDocument();
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function itRegistersUnmountCallback(subject: Function) {
|
|
34
|
-
test('registers callback that unmounts Component', () => {
|
|
35
|
-
const callbackSpy = jest.spyOn(ko.utils.domNodeDisposal, 'addDisposeCallback');
|
|
36
|
-
subject();
|
|
37
|
-
|
|
38
|
-
const root = ko.utils.domData.get(el, 'ko-binding-handler-root'); // Beware: this relies on an implementation detail
|
|
39
|
-
const unmountSpy = jest.spyOn(root, 'unmount');
|
|
40
|
-
|
|
41
|
-
const [, disposeCallback] = callbackSpy.mock.calls[0];
|
|
42
|
-
act(() => disposeCallback());
|
|
43
|
-
|
|
44
|
-
expect(unmountSpy).toHaveBeenCalled();
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function itRendersLegacyComponent(subject: Function) {
|
|
49
|
-
// eslint-disable-next-line react/no-deprecated
|
|
50
|
-
const originalRender = ReactDOM.render;
|
|
51
|
-
|
|
52
|
-
test('uses legacy API to render Component', async () => {
|
|
53
|
-
jest.spyOn(console, 'error').mockImplementation(jest.fn()); // suppress "ReactDOM.render deprecated" error
|
|
54
|
-
|
|
55
|
-
const renderSpy = jest
|
|
56
|
-
.spyOn(ReactDOM, 'render')
|
|
57
|
-
.mockImplementation((...args: Parameters<ReactDOM.Renderer>) =>
|
|
58
|
-
originalRender(...args)
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
subject();
|
|
62
|
-
|
|
63
|
-
await waitFor(() => {
|
|
64
|
-
expect(screen.getByRole('button', { name: 'MockButton' })).toBeInTheDocument();
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
expect(renderSpy).toHaveBeenCalled();
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function itRegistersLegacyUnmountCallback(subject: Function) {
|
|
72
|
-
test('uses legacy API to unmount Component', () => {
|
|
73
|
-
const callbackSpy = jest.spyOn(ko.utils.domNodeDisposal, 'addDisposeCallback');
|
|
74
|
-
|
|
75
|
-
subject();
|
|
76
|
-
|
|
77
|
-
jest.spyOn(ReactDOM, 'unmountComponentAtNode');
|
|
78
|
-
|
|
79
|
-
const [, disposeCallback] = callbackSpy.mock.calls[0];
|
|
80
|
-
disposeCallback();
|
|
81
|
-
|
|
82
|
-
// eslint-disable-next-line react/no-deprecated
|
|
83
|
-
expect(ReactDOM.unmountComponentAtNode).toHaveBeenCalledWith(el);
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
beforeEach(() => {
|
|
88
|
-
jest.clearAllMocks();
|
|
89
|
-
|
|
90
|
-
el = document.createElement('div');
|
|
91
|
-
document.body.appendChild(el);
|
|
92
|
-
|
|
93
|
-
mockAllBindings = new Map();
|
|
94
|
-
mockBindingContext = {};
|
|
95
|
-
valueAccessor = () => MockButton;
|
|
96
|
-
viewModel = {};
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
afterEach(() => {
|
|
100
|
-
el.remove();
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
function init(handler: KnockoutBindingHandler) {
|
|
104
|
-
let result: ReturnType<Required<KnockoutBindingHandler>['init']> = undefined;
|
|
105
|
-
|
|
106
|
-
act(() => {
|
|
107
|
-
result = handler.init!(
|
|
108
|
-
el,
|
|
109
|
-
valueAccessor,
|
|
110
|
-
mockAllBindings,
|
|
111
|
-
viewModel,
|
|
112
|
-
mockBindingContext
|
|
113
|
-
);
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
return result;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function update(handler: KnockoutBindingHandler) {
|
|
120
|
-
let result: ReturnType<Required<KnockoutBindingHandler>['update']> = undefined;
|
|
121
|
-
|
|
122
|
-
act(() => {
|
|
123
|
-
result = handler.update!(
|
|
124
|
-
el,
|
|
125
|
-
valueAccessor,
|
|
126
|
-
mockAllBindings,
|
|
127
|
-
viewModel,
|
|
128
|
-
mockBindingContext
|
|
129
|
-
);
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
return result;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
describe('reactUncontrolled', () => {
|
|
136
|
-
const handler: KnockoutBindingHandler = koBindingHandlers.reactUncontrolled(ko);
|
|
137
|
-
|
|
138
|
-
describe('init', () => {
|
|
139
|
-
const subject = () => init(handler);
|
|
140
|
-
|
|
141
|
-
itRendersComponent(subject);
|
|
142
|
-
itRegistersUnmountCallback(subject);
|
|
143
|
-
|
|
144
|
-
test('applies bindings to descendants', async () => {
|
|
145
|
-
jest.spyOn(ko, 'applyBindingsToDescendants');
|
|
146
|
-
|
|
147
|
-
expect(subject()).toEqual({ controlsDescendantBindings: true });
|
|
148
|
-
await waitFor(() =>
|
|
149
|
-
expect(ko.applyBindingsToDescendants).toHaveBeenCalledWith(
|
|
150
|
-
mockBindingContext,
|
|
151
|
-
el
|
|
152
|
-
)
|
|
153
|
-
);
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
describe('with {legacyRoot: true}', () => {
|
|
157
|
-
beforeEach(() => mockAllBindings.set('legacyRoot', true));
|
|
158
|
-
|
|
159
|
-
itRendersLegacyComponent(subject);
|
|
160
|
-
itRegistersLegacyUnmountCallback(subject);
|
|
161
|
-
});
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
describe('react', () => {
|
|
166
|
-
const handler: KnockoutBindingHandler = koBindingHandlers.react(ko);
|
|
167
|
-
|
|
168
|
-
describe('init', () => {
|
|
169
|
-
const subject = () => init(handler);
|
|
170
|
-
|
|
171
|
-
itRegistersUnmountCallback(subject);
|
|
172
|
-
|
|
173
|
-
describe('with {legacyRoot: true}', () => {
|
|
174
|
-
beforeEach(() => mockAllBindings.set('legacyRoot', true));
|
|
175
|
-
|
|
176
|
-
itRegistersLegacyUnmountCallback(subject);
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
describe('update', () => {
|
|
181
|
-
const subject = () => {
|
|
182
|
-
init(handler);
|
|
183
|
-
update(handler);
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
itRendersComponent(subject);
|
|
187
|
-
|
|
188
|
-
describe('with {legacyRoot: true}', () => {
|
|
189
|
-
beforeEach(() => mockAllBindings.set('legacyRoot', true));
|
|
190
|
-
|
|
191
|
-
itRendersLegacyComponent(subject);
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
describe('with props', () => {
|
|
195
|
-
const props = { title: 'Foo' };
|
|
196
|
-
|
|
197
|
-
beforeEach(() => mockAllBindings.set('props', props));
|
|
198
|
-
|
|
199
|
-
test('renders Component with props', () => {
|
|
200
|
-
subject();
|
|
201
|
-
|
|
202
|
-
expect(screen.getByRole('button', { name: props.title })).toBeInTheDocument();
|
|
203
|
-
});
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
describe('with viewModel', () => {
|
|
207
|
-
beforeEach(() => (viewModel = { title: 'Bar' }));
|
|
208
|
-
|
|
209
|
-
test('renders Component with viewModel as props', () => {
|
|
210
|
-
subject();
|
|
211
|
-
|
|
212
|
-
expect(
|
|
213
|
-
screen.getByRole('button', { name: viewModel.title })
|
|
214
|
-
).toBeInTheDocument();
|
|
215
|
-
});
|
|
216
|
-
});
|
|
217
|
-
});
|
|
218
|
-
});
|
|
219
|
-
});
|
|
@@ -1,172 +0,0 @@
|
|
|
1
|
-
import ko from 'knockout';
|
|
2
|
-
import { autorun } from 'mobx';
|
|
3
|
-
import { convertKoObservableToMobx, koObservableToMobx } from '../ko-observable-to-mobx';
|
|
4
|
-
|
|
5
|
-
test('Read a value', () => {
|
|
6
|
-
const koObservable = ko.observable('value');
|
|
7
|
-
|
|
8
|
-
const mobxObservable = convertKoObservableToMobx(koObservable);
|
|
9
|
-
|
|
10
|
-
expect(mobxObservable.get()).toBe('value');
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
test('Read a changed value', () => {
|
|
14
|
-
const koObservable = ko.observable('not changed');
|
|
15
|
-
|
|
16
|
-
const mobxObservable = convertKoObservableToMobx(koObservable);
|
|
17
|
-
|
|
18
|
-
koObservable('changed');
|
|
19
|
-
|
|
20
|
-
expect(mobxObservable.get()).toBe('changed');
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
test('Write a value', () => {
|
|
24
|
-
const koObservable = ko.observable('not changed');
|
|
25
|
-
|
|
26
|
-
const mobxObservable = convertKoObservableToMobx(koObservable);
|
|
27
|
-
|
|
28
|
-
mobxObservable.set('changed');
|
|
29
|
-
|
|
30
|
-
expect(koObservable()).toBe('changed');
|
|
31
|
-
expect(mobxObservable.get()).toBe('changed');
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test('Watch changes', () => {
|
|
35
|
-
const koObservable = ko.observable('first');
|
|
36
|
-
|
|
37
|
-
const mobxObservable = convertKoObservableToMobx(koObservable);
|
|
38
|
-
|
|
39
|
-
const changes: string[] = [];
|
|
40
|
-
const disposer = autorun(() => {
|
|
41
|
-
changes.push(mobxObservable.get());
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
koObservable('second');
|
|
45
|
-
|
|
46
|
-
expect(changes).toEqual(['first', 'second']);
|
|
47
|
-
|
|
48
|
-
disposer();
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
test('Watch ko.observableArray changes', () => {
|
|
52
|
-
const koObservableArray = ko.observableArray<string>([]);
|
|
53
|
-
|
|
54
|
-
const mobxObservable = convertKoObservableToMobx(koObservableArray);
|
|
55
|
-
|
|
56
|
-
const changes: string[][] = [];
|
|
57
|
-
const disposer = autorun(() => {
|
|
58
|
-
changes.push([...mobxObservable.get()]);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
koObservableArray.push('change');
|
|
62
|
-
koObservableArray.splice(0, 1);
|
|
63
|
-
|
|
64
|
-
expect(changes).toEqual([[], ['change'], []]);
|
|
65
|
-
|
|
66
|
-
disposer();
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test('Does not subscribe without observers', () => {
|
|
70
|
-
const koObservable = ko.observable('first');
|
|
71
|
-
|
|
72
|
-
const mobxObservable = convertKoObservableToMobx(koObservable);
|
|
73
|
-
|
|
74
|
-
koObservable('second');
|
|
75
|
-
|
|
76
|
-
expect(mobxObservable.get()).toBe('second');
|
|
77
|
-
expect(koObservable.getSubscriptionsCount()).toBe(0);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
test('Unsubscribe from ko.observable, then subscribe again', () => {
|
|
81
|
-
const koObservable = ko.observable('first');
|
|
82
|
-
|
|
83
|
-
const mobxObservable = convertKoObservableToMobx(koObservable);
|
|
84
|
-
|
|
85
|
-
// first case
|
|
86
|
-
const changes1: string[] = [];
|
|
87
|
-
const disposer1 = autorun(() => {
|
|
88
|
-
changes1.push(mobxObservable.get());
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
koObservable('second');
|
|
92
|
-
|
|
93
|
-
expect(changes1).toEqual(['first', 'second']);
|
|
94
|
-
|
|
95
|
-
expect(koObservable.getSubscriptionsCount()).toBe(1);
|
|
96
|
-
disposer1();
|
|
97
|
-
expect(koObservable.getSubscriptionsCount()).toBe(0);
|
|
98
|
-
|
|
99
|
-
// second case
|
|
100
|
-
const changes2: string[] = [];
|
|
101
|
-
const disposer2 = autorun(() => {
|
|
102
|
-
changes2.push(mobxObservable.get());
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
koObservable('third');
|
|
106
|
-
|
|
107
|
-
expect(changes2).toEqual(['second', 'third']);
|
|
108
|
-
|
|
109
|
-
expect(koObservable.getSubscriptionsCount()).toBe(1);
|
|
110
|
-
disposer2();
|
|
111
|
-
expect(koObservable.getSubscriptionsCount()).toBe(0);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
test('Read a value', () => {
|
|
115
|
-
const koObservable = ko.observable('value');
|
|
116
|
-
|
|
117
|
-
class Store {
|
|
118
|
-
@koObservableToMobx(() => koObservable)
|
|
119
|
-
declare value: string;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const instance = new Store();
|
|
123
|
-
|
|
124
|
-
expect(instance.value).toBe('value');
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
test('Write a value', () => {
|
|
128
|
-
const koObservable = ko.observable('not changed');
|
|
129
|
-
|
|
130
|
-
class Store {
|
|
131
|
-
@koObservableToMobx(() => koObservable)
|
|
132
|
-
declare value: string;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const instance = new Store();
|
|
136
|
-
|
|
137
|
-
instance.value = 'changed';
|
|
138
|
-
|
|
139
|
-
expect(koObservable()).toBe('changed');
|
|
140
|
-
expect(instance.value).toBe('changed');
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
test('Decorate when a ko.observable is not created yet', () => {
|
|
144
|
-
class Store {
|
|
145
|
-
@koObservableToMobx(() => koObservable)
|
|
146
|
-
declare value: string;
|
|
147
|
-
}
|
|
148
|
-
const instance = new Store();
|
|
149
|
-
|
|
150
|
-
const koObservable = ko.observable('success');
|
|
151
|
-
|
|
152
|
-
expect(instance.value).toBe('success');
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
test('Reassign ko.observable after `value` is read', () => {
|
|
156
|
-
let koObservable = ko.observable(true);
|
|
157
|
-
|
|
158
|
-
class Store {
|
|
159
|
-
@koObservableToMobx(() => koObservable)
|
|
160
|
-
declare value: string;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
const instance = new Store();
|
|
164
|
-
|
|
165
|
-
expect(instance.value).toBe(true);
|
|
166
|
-
|
|
167
|
-
koObservable = ko.observable(true);
|
|
168
|
-
|
|
169
|
-
koObservable(false);
|
|
170
|
-
|
|
171
|
-
expect(instance.value).toBe(false);
|
|
172
|
-
});
|