@vnejs/plugins.controllers.common.settings 0.0.0-stage → 0.2.17

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.
Files changed (37) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/index.js +13 -0
  3. package/dist/modules/fields.controller.arrows.d.ts +18 -0
  4. package/dist/modules/fields.controller.arrows.horizontal.d.ts +12 -0
  5. package/dist/modules/fields.controller.arrows.horizontal.js +11 -0
  6. package/dist/modules/fields.controller.arrows.js +33 -0
  7. package/dist/modules/fields.controller.d.ts +44 -0
  8. package/dist/modules/fields.controller.js +157 -0
  9. package/dist/modules/fields.view.d.ts +10 -0
  10. package/dist/modules/fields.view.js +9 -0
  11. package/dist/types.d.ts +23 -0
  12. package/dist/types.js +1 -0
  13. package/dist/utils/settings.d.ts +6 -0
  14. package/dist/utils/settings.js +1 -0
  15. package/dist/view/SettingsExampleText.d.ts +9 -0
  16. package/dist/view/SettingsExampleText.js +23 -0
  17. package/dist/view/SettingsTabContent.d.ts +15 -0
  18. package/dist/view/SettingsTabContent.js +32 -0
  19. package/dist/view/index.d.ts +3 -0
  20. package/dist/view/index.js +10 -0
  21. package/package.json +64 -4
  22. package/src/index.ts +17 -0
  23. package/src/modules/fields.controller.arrows.horizontal.ts +22 -0
  24. package/src/modules/fields.controller.arrows.ts +52 -0
  25. package/src/modules/fields.controller.ts +200 -0
  26. package/src/modules/fields.view.ts +23 -0
  27. package/src/tests/arrows.horizontal.test.ts +62 -0
  28. package/src/tests/arrows.test.ts +106 -0
  29. package/src/tests/controller.test.ts +235 -0
  30. package/src/tests/setup.ts +46 -0
  31. package/src/types.ts +32 -0
  32. package/src/utils/settings.ts +7 -0
  33. package/src/view/SettingsExampleText.tsx +60 -0
  34. package/src/view/SettingsTabContent.tsx +118 -0
  35. package/src/view/index.tsx +66 -0
  36. package/tsconfig.json +10 -0
  37. package/README.md +0 -3
@@ -0,0 +1,200 @@
1
+ import { ModuleController } from "@vnejs/module.components";
2
+
3
+ import type { SettingsField } from "@vnejs/contracts.controllers.common.settings";
4
+ import type { SettingsViewTabPayload } from "@vnejs/contracts.controllers.common.settings";
5
+
6
+ import type { SettingsViewTabContent } from "../utils/settings.js";
7
+
8
+ import type { SettingsViewPluginConstants, SettingsViewPluginEvents, SettingsViewPluginParams, SettingsViewPluginSettings } from "../types.js";
9
+ import type { SettingsViewPluginState } from "../types.js";
10
+
11
+ export class SettingsViewFieldsController extends ModuleController<
12
+ SettingsViewPluginEvents,
13
+ SettingsViewPluginConstants,
14
+ SettingsViewPluginSettings,
15
+ SettingsViewPluginParams,
16
+ SettingsViewPluginState
17
+ > {
18
+ name = "settings.view.fields.controller";
19
+
20
+ EVENT_UPDATE_VIEW = this.EVENTS.SETTINGS_VIEW.UPDATE;
21
+ EVENT_UPDATE_STATE = this.EVENTS.SETTINGS_VIEW.UPDATE_STATE;
22
+ EVENT_UPDATE_FAST = this.EVENTS.SETTINGS_VIEW.UPDATE_FAST;
23
+ EVENT_UPDATE_FORCE = this.EVENTS.SETTINGS_VIEW.UPDATE_FORCE;
24
+ EVENT_SHOW = this.EVENTS.SETTINGS_VIEW.SHOW;
25
+ EVENT_HIDE = this.EVENTS.SETTINGS_VIEW.HIDE;
26
+
27
+ CONTROLS = {
28
+ [this.CONST.CONTROLS.BUTTONS.INTERACT]: () => void this.onInteract(),
29
+ };
30
+ CONTROLS_INDEX = this.PARAMS.SETTINGS_VIEW.ZINDEX;
31
+ CONTROLS_CHECK_NEXT = true;
32
+
33
+ subscribe = () => {
34
+ super.subscribe();
35
+
36
+ this.on(this.EVENTS.SETTINGS_VIEW.TAB_SET, this.onTabSet);
37
+ this.on(this.EVENTS.SETTINGS_VIEW.ARROW_LEFT, this.onArrowLeft);
38
+ this.on(this.EVENTS.SETTINGS_VIEW.ARROW_RIGHT, this.onArrowRight);
39
+ };
40
+
41
+ onTabSet = async (payload: SettingsViewTabPayload = {}) => {
42
+ if (!this.state.isShow) return this.emit(this.EVENT_SHOW, payload);
43
+
44
+ const isForce = Boolean(payload.isForce || this.shared.viewForceAnimationSources?.isNotEmpty());
45
+
46
+ const next = { ...this.applyTab(payload), currentArrowVerticalItem: null as null };
47
+
48
+ if (isForce) return this.updateStateAndViewForce(next);
49
+
50
+ const key = next.tabKey!;
51
+ const alreadyMounted = Boolean(this.state.tabsContent?.[key]);
52
+
53
+ // Mount the incoming tab at opacity 0 first so CSS can crossfade both layers together.
54
+ if (!alreadyMounted) {
55
+ await this.updateStateAndViewFast({ tabsContent: next.tabsContent });
56
+ await this.waitRerender();
57
+ }
58
+
59
+ return this.updateStateAndView(next);
60
+ };
61
+
62
+ onArrowLeft = () => {
63
+ const field = this.getFocusedField();
64
+
65
+ if (!field) return;
66
+
67
+ if (field.options?.length) return this.shiftOptions(-1);
68
+
69
+ const { settingKey = "", step = 0, min = 0, type = "" } = field;
70
+ if (!settingKey) return;
71
+
72
+ if (type === "toggle") {
73
+ return this.shared.settings[settingKey] === true && void this.emit(this.EVENTS.SETTINGS.SET, { name: settingKey, value: false });
74
+ }
75
+
76
+ if (type === "range") {
77
+ const newValue = Number(Number(Number(this.shared.settings[settingKey]) - step).toFixed(4));
78
+
79
+ return newValue >= min && void this.emit(this.EVENTS.SETTINGS.SET, { name: settingKey, value: newValue });
80
+ }
81
+ };
82
+
83
+ onArrowRight = () => {
84
+ const field = this.getFocusedField();
85
+
86
+ if (!field) return;
87
+
88
+ if (field.options?.length) return this.shiftOptions(1);
89
+
90
+ const { settingKey = "", step = 0, max = 0, type = "" } = field;
91
+ if (!settingKey) return;
92
+
93
+ if (type === "toggle") {
94
+ return this.shared.settings[settingKey] === false && void this.emit(this.EVENTS.SETTINGS.SET, { name: settingKey, value: true });
95
+ }
96
+
97
+ if (type === "range") {
98
+ const newValue = Number(Number(Number(this.shared.settings[settingKey]) + step).toFixed(4));
99
+
100
+ return newValue <= max && void this.emit(this.EVENTS.SETTINGS.SET, { name: settingKey, value: newValue });
101
+ }
102
+ };
103
+
104
+ tabKeyOf = ({ tabKey, tab }: SettingsViewTabPayload = {}) => tabKey || tab?.titleLocKey || "default";
105
+ tabContentOf = (tab: SettingsViewTabPayload["tab"], locs?: Record<string, string>): SettingsViewTabContent => ({
106
+ fields: tab?.fields ?? [],
107
+ showTextExample: Boolean(tab?.showTextExample),
108
+ locs,
109
+ });
110
+ applyTab = (payload: SettingsViewTabPayload = {}) => {
111
+ const { tab, locs } = payload;
112
+ const fields = tab?.fields ?? [];
113
+ const key = this.tabKeyOf(payload);
114
+
115
+ const tabsContent = payload.tabs
116
+ ? Object.fromEntries(Object.entries(payload.tabs).map(([tabKey, settingsTab]) => [tabKey, this.tabContentOf(settingsTab, locs)]))
117
+ : { ...(this.state.tabsContent ?? {}), [key]: this.tabContentOf(tab, locs) };
118
+
119
+ return {
120
+ tab,
121
+ tabKey: key,
122
+ tabsContent,
123
+ fields,
124
+ locs,
125
+ showTextExample: Boolean(tab?.showTextExample),
126
+ currentItemInner: 0,
127
+ };
128
+ };
129
+ onUpdateState = (payload: Partial<SettingsViewPluginState> = {}) => {
130
+ const currentArrowVerticalItem = payload.currentArrowVerticalItem;
131
+ const field = currentArrowVerticalItem == null ? undefined : this.state.fields?.[currentArrowVerticalItem];
132
+
133
+ this.updateState({ ...payload, currentItemInner: this.innerIndexOfField(field) });
134
+ };
135
+ getFocusedField = () => {
136
+ const { currentArrowVerticalItem, fields = [] } = this.state;
137
+
138
+ if (currentArrowVerticalItem == null || !fields.length) return;
139
+
140
+ return fields[currentArrowVerticalItem];
141
+ };
142
+ innerIndexOfField = (field?: SettingsField) => {
143
+ const options = field?.options ?? [];
144
+ if (!options.length) return 0;
145
+
146
+ const settingKey = field?.settingKey;
147
+ if (!settingKey) return 0;
148
+
149
+ const index = options.findIndex((option) => option.value === this.shared.settings[settingKey]);
150
+
151
+ return index >= 0 ? index : 0;
152
+ };
153
+ optionIndexOf = (length: number, index = 0) => {
154
+ if (!length) return 0;
155
+
156
+ return ((index % length) + length) % length;
157
+ };
158
+ shiftOptions = (direction: -1 | 1) => {
159
+ const field = this.getFocusedField();
160
+ const options = field?.options ?? [];
161
+
162
+ if (!options.length) return;
163
+
164
+ const currentItemInner = this.optionIndexOf(options.length, (this.state.currentItemInner ?? 0) + direction);
165
+ const option = options[currentItemInner];
166
+ const settingKey = field?.settingKey;
167
+
168
+ void this.updateStateAndViewFast({ currentItemInner });
169
+
170
+ if (settingKey && option && !option.action) {
171
+ void this.emit(this.EVENTS.SETTINGS.SET, { name: settingKey, value: option.value });
172
+ }
173
+ };
174
+ onInteract = () => {
175
+ const field = this.getFocusedField();
176
+
177
+ if (!field) return;
178
+
179
+ const { settingKey = "", type = "", options = [] } = field;
180
+ const { locs = {} } = this.state;
181
+
182
+ if (options?.length) {
183
+ const option = options[this.optionIndexOf(options.length, this.state.currentItemInner ?? 0)];
184
+
185
+ if (!option) return;
186
+
187
+ if (option.action) return this.PARAMS.SETTINGS_VIEW.ACTIONS[option.action](this, locs);
188
+ if (settingKey) return void this.emit(this.EVENTS.SETTINGS.SET, { name: settingKey, value: option.value });
189
+
190
+ return;
191
+ }
192
+
193
+ if (!settingKey) return;
194
+
195
+ if (type === "toggle") return void this.emit(this.EVENTS.SETTINGS.SET, { name: settingKey, value: !this.shared.settings[settingKey] });
196
+ };
197
+ onShowBefore = async (payload: SettingsViewTabPayload = {}) => {
198
+ return this.updateState(this.applyTab(payload));
199
+ };
200
+ }
@@ -0,0 +1,23 @@
1
+ import { ModuleView } from "@vnejs/module.components";
2
+
3
+ import type { SettingsViewPluginConstants, SettingsViewPluginEvents, SettingsViewPluginParams, SettingsViewPluginSettings } from "../types.js";
4
+ import type { SettingsViewPluginState } from "../types.js";
5
+
6
+ import { render } from "../view/index.js";
7
+ export class SettingsViewFieldsView extends ModuleView<
8
+ SettingsViewPluginEvents,
9
+ SettingsViewPluginConstants,
10
+ SettingsViewPluginSettings,
11
+ SettingsViewPluginParams,
12
+ SettingsViewPluginState
13
+ > {
14
+ name = "settings.view.fields.view";
15
+
16
+ EVENT_UPDATE_VIEW = this.EVENTS.SETTINGS_VIEW.UPDATE;
17
+
18
+ LOC_LABEL = this.PARAMS.SETTINGS_VIEW.LOC_LABEL;
19
+
20
+ TRANSITION = this.PARAMS.SETTINGS_VIEW.TRANSITION;
21
+
22
+ renderFunc = render;
23
+ }
@@ -0,0 +1,62 @@
1
+ import { beforeEach, describe, expect, it } from "vitest";
2
+
3
+ import { spyEvent } from "@vnejs/test-utils";
4
+
5
+ import { SettingsViewFieldsControllerArrowsHorizontal } from "../modules/fields.controller.arrows.horizontal.js";
6
+
7
+ import { CONTROLS_EVENTS, createSettingsViewTestModule, registerSettingsViewPluginVne, SUBSCRIBE_EVENTS } from "./setup.js";
8
+
9
+ describe("SettingsViewFieldsControllerArrowsHorizontal", () => {
10
+ beforeEach(() => {
11
+ registerSettingsViewPluginVne();
12
+ });
13
+
14
+ it("SHOW pushes arrow controls above the controller layer", async () => {
15
+ const { module, observer } = createSettingsViewTestModule(SettingsViewFieldsControllerArrowsHorizontal);
16
+ const arrows = module as SettingsViewFieldsControllerArrowsHorizontal;
17
+ const push = spyEvent(observer, CONTROLS_EVENTS.PUSH);
18
+
19
+ await observer.emit(SUBSCRIBE_EVENTS.SHOW);
20
+
21
+ expect(arrows.currentItem).toBeNull();
22
+ expect(push).toHaveBeenCalledExactlyOnceWith(
23
+ expect.objectContaining({
24
+ key: "settings.view.fields.controller.arrows.horizontal",
25
+ checkNext: true,
26
+ index: arrows.PARAMS.SETTINGS_VIEW.ZINDEX + 1,
27
+ }),
28
+ );
29
+ expect(push.mock.calls[0][0].controls).toHaveProperty(arrows.CONST.CONTROLS.BUTTONS.ARROW_LEFT);
30
+ expect(push.mock.calls[0][0].controls).toHaveProperty(arrows.CONST.CONTROLS.BUTTONS.ARROW_RIGHT);
31
+ });
32
+
33
+ it("HIDE pops arrow controls", async () => {
34
+ const { observer } = createSettingsViewTestModule(SettingsViewFieldsControllerArrowsHorizontal);
35
+ const pop = spyEvent(observer, CONTROLS_EVENTS.POP);
36
+
37
+ await observer.emit(SUBSCRIBE_EVENTS.SHOW);
38
+ await observer.emit(SUBSCRIBE_EVENTS.HIDE);
39
+
40
+ expect(pop).toHaveBeenCalledExactlyOnceWith({ key: "settings.view.fields.controller.arrows.horizontal" });
41
+ });
42
+
43
+ it("onArrowLeft emits ARROW_LEFT", async () => {
44
+ const { module, observer } = createSettingsViewTestModule(SettingsViewFieldsControllerArrowsHorizontal);
45
+ const arrows = module as SettingsViewFieldsControllerArrowsHorizontal;
46
+ const arrowLeft = spyEvent(observer, SUBSCRIBE_EVENTS.ARROW_LEFT);
47
+
48
+ arrows.onArrowLeft();
49
+
50
+ expect(arrowLeft).toHaveBeenCalledOnce();
51
+ });
52
+
53
+ it("onArrowRight emits ARROW_RIGHT", async () => {
54
+ const { module, observer } = createSettingsViewTestModule(SettingsViewFieldsControllerArrowsHorizontal);
55
+ const arrows = module as SettingsViewFieldsControllerArrowsHorizontal;
56
+ const arrowRight = spyEvent(observer, SUBSCRIBE_EVENTS.ARROW_RIGHT);
57
+
58
+ arrows.onArrowRight();
59
+
60
+ expect(arrowRight).toHaveBeenCalledOnce();
61
+ });
62
+ });
@@ -0,0 +1,106 @@
1
+ import { beforeEach, describe, expect, it } from "vitest";
2
+
3
+ import { spyEvent } from "@vnejs/test-utils";
4
+
5
+ import { SettingsViewFieldsControllerArrows } from "../modules/fields.controller.arrows.js";
6
+
7
+ import { CONTROLS_EVENTS, createSettingsViewTestModule, registerSettingsViewPluginVne, SUBSCRIBE_EVENTS } from "./setup.js";
8
+
9
+ const generalTab = {
10
+ titleLocKey: "general",
11
+ fields: [
12
+ { settingKey: "a", type: "toggle" },
13
+ { settingKey: "b", type: "toggle" },
14
+ ],
15
+ };
16
+
17
+ describe("SettingsViewFieldsControllerArrows", () => {
18
+ beforeEach(() => {
19
+ registerSettingsViewPluginVne();
20
+ });
21
+
22
+ it("SHOW pushes arrow controls above the controller layer", async () => {
23
+ const { module, observer } = createSettingsViewTestModule(SettingsViewFieldsControllerArrows);
24
+ const arrows = module as SettingsViewFieldsControllerArrows;
25
+ const push = spyEvent(observer, CONTROLS_EVENTS.PUSH);
26
+
27
+ await observer.emit(SUBSCRIBE_EVENTS.SHOW, { tab: generalTab });
28
+
29
+ expect(arrows.currentItem).toBeNull();
30
+ expect(arrows.fields).toHaveLength(2);
31
+ expect(push).toHaveBeenCalledExactlyOnceWith(
32
+ expect.objectContaining({
33
+ key: "settings.view.fields.controller.arrows",
34
+ checkNext: true,
35
+ index: arrows.PARAMS.SETTINGS_VIEW.ZINDEX + 1,
36
+ }),
37
+ );
38
+ });
39
+
40
+ it("HIDE pops arrow controls", async () => {
41
+ const { observer } = createSettingsViewTestModule(SettingsViewFieldsControllerArrows);
42
+ const pop = spyEvent(observer, CONTROLS_EVENTS.POP);
43
+
44
+ await observer.emit(SUBSCRIBE_EVENTS.SHOW, { tab: generalTab });
45
+ await observer.emit(SUBSCRIBE_EVENTS.HIDE);
46
+
47
+ expect(pop).toHaveBeenCalledExactlyOnceWith({ key: "settings.view.fields.controller.arrows" });
48
+ });
49
+
50
+ it("first ARROW_BOTTOM selects 0 and emits UPDATE_STATE plus UPDATE_FAST", async () => {
51
+ const { module, observer } = createSettingsViewTestModule(SettingsViewFieldsControllerArrows);
52
+ const arrows = module as SettingsViewFieldsControllerArrows;
53
+ const stateUpdate = spyEvent(observer, SUBSCRIBE_EVENTS.UPDATE_STATE);
54
+ const updateFast = spyEvent(observer, SUBSCRIBE_EVENTS.UPDATE_FAST);
55
+
56
+ await observer.emit(SUBSCRIBE_EVENTS.SHOW, { tab: generalTab });
57
+ await arrows.incCurrentItem();
58
+
59
+ expect(arrows.currentItem).toBe(0);
60
+ expect(stateUpdate).toHaveBeenCalledExactlyOnceWith({ currentArrowVerticalItem: 0 });
61
+ expect(updateFast).toHaveBeenCalledOnce();
62
+ });
63
+
64
+ it("arrow wrap cycles fields", async () => {
65
+ const { module, observer } = createSettingsViewTestModule(SettingsViewFieldsControllerArrows);
66
+ const arrows = module as SettingsViewFieldsControllerArrows;
67
+ const stateUpdate = spyEvent(observer, SUBSCRIBE_EVENTS.UPDATE_STATE);
68
+
69
+ await observer.emit(SUBSCRIBE_EVENTS.SHOW, { tab: generalTab });
70
+ await arrows.incCurrentItem();
71
+ await arrows.incCurrentItem();
72
+ expect(arrows.currentItem).toBe(1);
73
+
74
+ await arrows.incCurrentItem();
75
+ expect(arrows.currentItem).toBe(0);
76
+ expect(stateUpdate).toHaveBeenLastCalledWith({ currentArrowVerticalItem: 0 });
77
+
78
+ await arrows.decCurrentItem();
79
+ expect(arrows.currentItem).toBe(1);
80
+ });
81
+
82
+ it("TAB_SET resets currentItem and syncs fields", async () => {
83
+ const { module, observer } = createSettingsViewTestModule(SettingsViewFieldsControllerArrows);
84
+ const arrows = module as SettingsViewFieldsControllerArrows;
85
+ const stateUpdate = spyEvent(observer, SUBSCRIBE_EVENTS.UPDATE_STATE);
86
+
87
+ await observer.emit(SUBSCRIBE_EVENTS.SHOW, { tab: generalTab });
88
+ await arrows.incCurrentItem();
89
+ expect(arrows.currentItem).toBe(0);
90
+
91
+ await observer.emit(SUBSCRIBE_EVENTS.TAB_SET, {
92
+ tab: {
93
+ titleLocKey: "audio",
94
+ fields: [
95
+ { settingKey: "c", type: "toggle" },
96
+ { settingKey: "d", type: "toggle" },
97
+ { settingKey: "e", type: "toggle" },
98
+ ],
99
+ },
100
+ });
101
+
102
+ expect(arrows.currentItem).toBeNull();
103
+ expect(arrows.fields).toHaveLength(3);
104
+ expect(stateUpdate).toHaveBeenLastCalledWith({ currentArrowVerticalItem: null });
105
+ });
106
+ });
@@ -0,0 +1,235 @@
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+
3
+ import { spyEvent } from "@vnejs/test-utils";
4
+
5
+ import { SUBSCRIBE_EVENTS as SETTINGS_EVENTS } from "@vnejs/contracts.core.settings";
6
+
7
+ import { SettingsViewFieldsController } from "../modules/fields.controller.js";
8
+
9
+ import { createSettingsViewTestModule, registerSettingsViewPluginVne, SUBSCRIBE_EVENTS } from "./setup.js";
10
+
11
+ describe("SettingsViewFieldsController", () => {
12
+ beforeEach(() => {
13
+ registerSettingsViewPluginVne();
14
+ });
15
+
16
+ it("SHOW stores fields from tab", async () => {
17
+ const { module, observer } = createSettingsViewTestModule(SettingsViewFieldsController);
18
+
19
+ await observer.emit(SUBSCRIBE_EVENTS.SHOW, {
20
+ tabKey: "general",
21
+ tab: {
22
+ titleLocKey: "general",
23
+ fields: [
24
+ { settingKey: "a", type: "toggle" },
25
+ { settingKey: "b", type: "toggle" },
26
+ ],
27
+ },
28
+ });
29
+
30
+ expect((module as SettingsViewFieldsController).state.fields).toHaveLength(2);
31
+ expect((module as SettingsViewFieldsController).state.isShow).toBe(true);
32
+ expect((module as SettingsViewFieldsController).state.tabKey).toBe("general");
33
+ expect(Object.keys((module as SettingsViewFieldsController).state.tabsContent ?? {})).toEqual(["general"]);
34
+ });
35
+
36
+ it("ARROW_RIGHT toggles setting when focused", async () => {
37
+ const { module, observer } = createSettingsViewTestModule(SettingsViewFieldsController, {
38
+ shared: { settings: { "test.toggle": false } },
39
+ });
40
+
41
+ await observer.emit(SUBSCRIBE_EVENTS.SHOW, {
42
+ tab: { titleLocKey: "general", fields: [{ settingKey: "test.toggle", type: "toggle" }] },
43
+ });
44
+
45
+ (module as SettingsViewFieldsController).updateState({ currentArrowVerticalItem: 0 });
46
+
47
+ const settingsSet = spyEvent(observer, SETTINGS_EVENTS.SET);
48
+
49
+ (module as SettingsViewFieldsController).onArrowRight();
50
+
51
+ expect(settingsSet).toHaveBeenCalledExactlyOnceWith({ name: "test.toggle", value: true });
52
+ });
53
+
54
+ it("TAB_SET while visible replaces fields with fade", async () => {
55
+ const { module, observer } = createSettingsViewTestModule(SettingsViewFieldsController);
56
+
57
+ await observer.emit(SUBSCRIBE_EVENTS.SHOW, {
58
+ tabKey: "general",
59
+ tabs: {
60
+ general: { titleLocKey: "general", fields: [{ settingKey: "a", type: "toggle" }] },
61
+ audio: {
62
+ titleLocKey: "audio",
63
+ showTextExample: true,
64
+ fields: [
65
+ { settingKey: "b", type: "toggle" },
66
+ { settingKey: "c", type: "toggle" },
67
+ ],
68
+ },
69
+ },
70
+ tab: { titleLocKey: "general", fields: [{ settingKey: "a", type: "toggle" }] },
71
+ });
72
+
73
+ await observer.emit(SUBSCRIBE_EVENTS.TAB_SET, {
74
+ isForce: true,
75
+ tabKey: "audio",
76
+ tab: {
77
+ titleLocKey: "audio",
78
+ showTextExample: true,
79
+ fields: [
80
+ { settingKey: "b", type: "toggle" },
81
+ { settingKey: "c", type: "toggle" },
82
+ ],
83
+ },
84
+ });
85
+
86
+ expect((module as SettingsViewFieldsController).state.fields).toHaveLength(2);
87
+ expect((module as SettingsViewFieldsController).state.showTextExample).toBe(true);
88
+ expect((module as SettingsViewFieldsController).state.currentItemInner).toBe(0);
89
+ expect((module as SettingsViewFieldsController).state.currentArrowVerticalItem).toBeNull();
90
+ expect((module as SettingsViewFieldsController).state.tabKey).toBe("audio");
91
+ expect(Object.keys((module as SettingsViewFieldsController).state.tabsContent ?? {}).sort()).toEqual(["audio", "general"]);
92
+ });
93
+
94
+ it("ARROW_RIGHT on selector_texts cycles inner and SET", async () => {
95
+ const { module, observer } = createSettingsViewTestModule(SettingsViewFieldsController, {
96
+ shared: { settings: { "test.lang": "ru" } },
97
+ });
98
+
99
+ await observer.emit(SUBSCRIBE_EVENTS.SHOW, {
100
+ tab: {
101
+ titleLocKey: "general",
102
+ fields: [
103
+ {
104
+ settingKey: "test.lang",
105
+ type: "selector_texts",
106
+ options: [
107
+ { text: "ru", value: "ru" },
108
+ { text: "en", value: "en" },
109
+ { text: "de", value: "de" },
110
+ ],
111
+ },
112
+ ],
113
+ },
114
+ });
115
+
116
+ const controller = module as SettingsViewFieldsController;
117
+
118
+ controller.updateState({ currentArrowVerticalItem: 0, currentItemInner: 0 });
119
+
120
+ const settingsSet = spyEvent(observer, SETTINGS_EVENTS.SET);
121
+
122
+ controller.onArrowRight();
123
+
124
+ expect(controller.state.currentItemInner).toBe(1);
125
+ expect(settingsSet).toHaveBeenCalledExactlyOnceWith({ name: "test.lang", value: "en" });
126
+ });
127
+
128
+ it("ARROW_LEFT on selector_texts wraps inner and SET", async () => {
129
+ const { module, observer } = createSettingsViewTestModule(SettingsViewFieldsController, {
130
+ shared: { settings: { "test.lang": "ru" } },
131
+ });
132
+
133
+ await observer.emit(SUBSCRIBE_EVENTS.SHOW, {
134
+ tab: {
135
+ titleLocKey: "general",
136
+ fields: [
137
+ {
138
+ settingKey: "test.lang",
139
+ type: "selector_texts",
140
+ options: [
141
+ { text: "ru", value: "ru" },
142
+ { text: "en", value: "en" },
143
+ { text: "de", value: "de" },
144
+ ],
145
+ },
146
+ ],
147
+ },
148
+ });
149
+
150
+ const controller = module as SettingsViewFieldsController;
151
+
152
+ controller.updateState({ currentArrowVerticalItem: 0, currentItemInner: 0 });
153
+
154
+ const settingsSet = spyEvent(observer, SETTINGS_EVENTS.SET);
155
+
156
+ controller.onArrowLeft();
157
+
158
+ expect(controller.state.currentItemInner).toBe(2);
159
+ expect(settingsSet).toHaveBeenCalledExactlyOnceWith({ name: "test.lang", value: "de" });
160
+ });
161
+
162
+ it("ARROW_* on actions without settingKey only moves inner", async () => {
163
+ const { module, observer } = createSettingsViewTestModule(SettingsViewFieldsController);
164
+ const controller = module as SettingsViewFieldsController;
165
+ const doA = vi.fn();
166
+ const doB = vi.fn();
167
+
168
+ controller.PARAMS.SETTINGS_VIEW.ACTIONS.do_a = doA;
169
+ controller.PARAMS.SETTINGS_VIEW.ACTIONS.do_b = doB;
170
+
171
+ await observer.emit(SUBSCRIBE_EVENTS.SHOW, {
172
+ tab: {
173
+ titleLocKey: "general",
174
+ fields: [
175
+ {
176
+ type: "actions",
177
+ options: [
178
+ { locKey: "a", action: "do_a" },
179
+ { locKey: "b", action: "do_b" },
180
+ ],
181
+ },
182
+ ],
183
+ },
184
+ });
185
+
186
+ controller.updateState({ currentArrowVerticalItem: 0, currentItemInner: 0 });
187
+
188
+ const settingsSet = spyEvent(observer, SETTINGS_EVENTS.SET);
189
+
190
+ controller.onArrowLeft();
191
+
192
+ expect(controller.state.currentItemInner).toBe(1);
193
+ expect(doA).not.toHaveBeenCalled();
194
+ expect(doB).not.toHaveBeenCalled();
195
+ expect(settingsSet).not.toHaveBeenCalled();
196
+
197
+ controller.onInteract();
198
+
199
+ expect(doB).toHaveBeenCalledOnce();
200
+ expect(doA).not.toHaveBeenCalled();
201
+ });
202
+
203
+ it("UPDATE_STATE syncs currentItemInner to the current option", async () => {
204
+ const { module, observer } = createSettingsViewTestModule(SettingsViewFieldsController, {
205
+ shared: { settings: { "test.lang": "en" } },
206
+ });
207
+
208
+ await observer.emit(SUBSCRIBE_EVENTS.SHOW, {
209
+ tab: {
210
+ titleLocKey: "general",
211
+ fields: [
212
+ { settingKey: "a", type: "toggle" },
213
+ {
214
+ settingKey: "test.lang",
215
+ type: "selector_texts",
216
+ options: [
217
+ { text: "ru", value: "ru" },
218
+ { text: "en", value: "en" },
219
+ { text: "de", value: "de" },
220
+ ],
221
+ },
222
+ ],
223
+ },
224
+ });
225
+
226
+ const controller = module as SettingsViewFieldsController;
227
+
228
+ controller.updateState({ currentArrowVerticalItem: 0, currentItemInner: 99 });
229
+
230
+ await observer.emit(SUBSCRIBE_EVENTS.UPDATE_STATE, { currentArrowVerticalItem: 1 });
231
+
232
+ expect(controller.state.currentArrowVerticalItem).toBe(1);
233
+ expect(controller.state.currentItemInner).toBe(1);
234
+ });
235
+ });
@@ -0,0 +1,46 @@
1
+ import { SourcesSet } from "@vnejs/sources-set";
2
+ import { createTestModule as baseCreateTestModule, registerCoreVne, registerVnePlugin, stubSilentEvent } from "@vnejs/test-utils";
3
+
4
+ import { CONSTANTS, PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/contracts.controllers.common.settings";
5
+ import { CONSTANTS as CONTROLS_CONST, PLUGIN_NAME as CONTROLS, SUBSCRIBE_EVENTS as CONTROLS_EVENTS } from "@vnejs/contracts.controls";
6
+
7
+ export const registerSettingsViewPluginVne = () => {
8
+ registerCoreVne();
9
+ registerVnePlugin(CONTROLS, { constants: CONTROLS_CONST, events: CONTROLS_EVENTS });
10
+ registerVnePlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, params: PARAMS, constants: CONSTANTS });
11
+ };
12
+
13
+ export const createSettingsViewTestModule = <T extends Parameters<typeof baseCreateTestModule>[0]>(
14
+ ModuleClass: T,
15
+ options: Parameters<typeof baseCreateTestModule>[1] = {},
16
+ ) => {
17
+ const { shared: sharedOption, state: stateOption, ...rest } = options;
18
+
19
+ const result = baseCreateTestModule(ModuleClass, {
20
+ ...rest,
21
+ state: {
22
+ "settings.view.fields.controller": { isShow: false, fields: [], currentArrowVerticalItem: null, currentItemInner: 0 },
23
+ ...(stateOption ?? {}),
24
+ },
25
+ shared: {
26
+ ...(sharedOption ?? {}),
27
+ settings: { ...(sharedOption?.settings ?? {}) },
28
+ viewForceAnimationSources: new SourcesSet(),
29
+ },
30
+ });
31
+
32
+ stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.SHOW);
33
+ stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.HIDE);
34
+ stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.UPDATE);
35
+ stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.UPDATE_FAST);
36
+ stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.UPDATE_STATE);
37
+ stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.TAB_SET);
38
+ stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.ARROW_LEFT);
39
+ stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.ARROW_RIGHT);
40
+ stubSilentEvent(result.observer, CONTROLS_EVENTS.PUSH);
41
+ stubSilentEvent(result.observer, CONTROLS_EVENTS.POP);
42
+
43
+ return result;
44
+ };
45
+
46
+ export { SUBSCRIBE_EVENTS, PARAMS, PLUGIN_NAME, CONTROLS_EVENTS };