• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

naver / egjs-flicking / 10557177632

26 Aug 2024 09:22AM UTC coverage: 38.327% (-44.5%) from 82.855%
10557177632

Pull #886

github

daybrush
fix: recalculate camera offset
Pull Request #886: fix: recalculate camera offset

2039 of 7372 branches covered (27.66%)

Branch coverage included in aggregate %.

11 of 29 new or added lines in 2 files covered. (37.93%)

5575 existing lines in 46 files now uncovered.

5099 of 11252 relevant lines covered (45.32%)

10.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

13.79
/test/unit/control/StrictControl.spec.ts
1
import StrictControl from "~/control/StrictControl";
1✔
2
import FlickingError from "~/core/FlickingError";
1!
3
import * as ERROR from "~/const/error";
×
4
import { MOVE_TYPE } from "~/const/external";
×
5

6
import El from "../helper/El";
×
7
import { createFlicking, simulate, tick } from "../helper/test-util";
×
8

9
describe("StrictControl", () => {
1!
10
  describe("Methods", () => {
×
11
    describe("updateInput", () => {
12
      it("should set controller range to prev panel's position to next panel's position", async () => {
×
13
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, { moveType: MOVE_TYPE.STRICT, defaultIndex: 1 });
14
        const prevPanel = flicking.panels[0];
1✔
15
        const nextPanel = flicking.panels[2];
1!
16
        const controller = flicking.control.controller;
×
17

×
18
        flicking.control.updateInput();
×
19

×
20
        expect(controller.range[0]).to.equal(prevPanel.position);
21
        expect(controller.range[1]).to.equal(nextPanel.position);
1✔
UNCOV
22
      });
×
UNCOV
23

×
UNCOV
24
      it("should set control range to current panel's position to next panel's position if current panel is the first panel", async () => {
×
25
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, { moveType: MOVE_TYPE.STRICT, defaultIndex: 0 });
×
UNCOV
26
        const currentPanel = flicking.panels[0];
×
UNCOV
27
        const nextPanel = flicking.panels[1];
×
28
        const controller = flicking.control.controller;
29

30
        flicking.control.updateInput();
1✔
UNCOV
31

×
UNCOV
32
        expect(controller.range[0]).to.equal(currentPanel.position);
×
UNCOV
33
        expect(controller.range[1]).to.equal(nextPanel.position);
×
34
      });
UNCOV
35

×
UNCOV
36
      it("should set control range to prev panel's position to current panel's position if current panel is the last panel", async () => {
×
UNCOV
37
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, { moveType: MOVE_TYPE.STRICT, defaultIndex: 2 });
×
UNCOV
38
        const prevPanel = flicking.panels[1];
×
UNCOV
39
        const currentPanel = flicking.panels[2];
×
UNCOV
40
        const controller = flicking.control.controller;
×
UNCOV
41

×
42
        flicking.control.updateInput();
×
43

×
44
        expect(controller.range[0]).to.equal(prevPanel.position);
UNCOV
45
        expect(controller.range[1]).to.equal(currentPanel.position);
×
46
      });
×
47

×
48
      it("should set control range to current - n panel's position to current + n panel's position", async () => {
×
49
        const flicking = await createFlicking(
×
50
          El.viewport("1000px", "100%").add(
×
51
            El.camera().add(
UNCOV
52
              // Total 9 panels
×
UNCOV
53
              El.panel().setWidth("100%").setHeight(300),
×
UNCOV
54
              El.panel().setWidth("100%").setHeight(300),
×
55
              El.panel().setWidth("100%").setHeight(300),
56
              El.panel().setWidth("100%").setHeight(300),
57
              El.panel().setWidth("100%").setHeight(300),
1✔
58
              El.panel().setWidth("100%").setHeight(300),
3!
59
              El.panel().setWidth("100%").setHeight(300),
60
              El.panel().setWidth("100%").setHeight(300),
1✔
61
              El.panel().setWidth("100%").setHeight(300)
1✔
62
            ),
1✔
63
          ),
1✔
64
          { moveType: [MOVE_TYPE.STRICT, { count: 2 }] }
1✔
65
        );
1✔
66
        const panels = flicking.panels;
1✔
67
        const controller = flicking.control.controller;
1✔
68

1✔
69
        await flicking.moveTo(2, 0);
1✔
70

1✔
71
        expect(controller.range[0]).to.equal(panels[0].position);
UNCOV
72
        expect(controller.range[1]).to.equal(panels[4].position);
×
UNCOV
73

×
UNCOV
74
        await flicking.moveTo(4, 0);
×
75

UNCOV
76
        expect(controller.range[0]).to.equal(panels[2].position);
×
UNCOV
77
        expect(controller.range[1]).to.equal(panels[6].position);
×
UNCOV
78

×
UNCOV
79
        await flicking.moveTo(6, 0);
×
UNCOV
80

×
UNCOV
81
        expect(controller.range[0]).to.equal(panels[4].position);
×
UNCOV
82
        expect(controller.range[1]).to.equal(panels[8].position);
×
UNCOV
83
      });
×
84

85
      it("should set control range to current - n panel's position to current + n panel's position (edge case)", async () => {
86
        const flicking = await createFlicking(
87
          El.viewport("1000px", "100%").add(
1✔
88
            El.camera().add(
UNCOV
89
              // Total 9 panels
×
UNCOV
90
              El.panel().setWidth("100%").setHeight(300),
×
UNCOV
91
              El.panel().setWidth("100%").setHeight(300),
×
92
              El.panel().setWidth("100%").setHeight(300),
UNCOV
93
              El.panel().setWidth("100%").setHeight(300),
×
UNCOV
94
              El.panel().setWidth("100%").setHeight(300),
×
UNCOV
95
              El.panel().setWidth("100%").setHeight(300),
×
UNCOV
96
              El.panel().setWidth("100%").setHeight(300),
×
UNCOV
97
              El.panel().setWidth("100%").setHeight(300),
×
UNCOV
98
              El.panel().setWidth("100%").setHeight(300)
×
UNCOV
99
            ),
×
UNCOV
100
          ),
×
101
          { moveType: [MOVE_TYPE.STRICT, { count: 2 }] }
102
        );
103
        const panels = flicking.panels;
104
        const controller = flicking.control.controller;
1✔
105

UNCOV
106
        await flicking.moveTo(0, 0);
×
UNCOV
107

×
UNCOV
108
        expect(controller.range[0]).to.equal(panels[0].position);
×
109
        expect(controller.range[1]).to.equal(panels[2].position);
UNCOV
110

×
UNCOV
111
        await flicking.moveTo(1, 0);
×
UNCOV
112

×
UNCOV
113
        expect(controller.range[0]).to.equal(panels[0].position);
×
UNCOV
114
        expect(controller.range[1]).to.equal(panels[3].position);
×
UNCOV
115

×
UNCOV
116
        await flicking.moveTo(8, 0);
×
UNCOV
117

×
118
        expect(controller.range[0]).to.equal(panels[6].position);
119
        expect(controller.range[1]).to.equal(panels[8].position);
120

121
        await flicking.moveTo(7, 0);
1✔
122

UNCOV
123
        expect(controller.range[0]).to.equal(panels[5].position);
×
UNCOV
124
        expect(controller.range[1]).to.equal(panels[8].position);
×
UNCOV
125
      });
×
126

127
      it("should clamp controller range to camera range if bound=true", async () => {
128
        const flicking = await createFlicking(
UNCOV
129
          El.viewport("1000px", "100%").add(
×
UNCOV
130
            El.camera().add(
×
UNCOV
131
              El.panel().setWidth("50%").setHeight(300),
×
UNCOV
132
              El.panel().setWidth("50%").setHeight(300),
×
133
              El.panel().setWidth("50%").setHeight(300)
UNCOV
134
            ),
×
UNCOV
135
          ),
×
UNCOV
136
          { moveType: MOVE_TYPE.STRICT, bound: true, defaultIndex: 1 }
×
UNCOV
137
        );
×
138
        const camera = flicking.camera;
UNCOV
139
        const control = flicking.control;
×
UNCOV
140
        const controller = control.controller;
×
UNCOV
141
        const prevPanel = flicking.panels[0];
×
UNCOV
142
        const nextPanel = flicking.panels[2];
×
143

UNCOV
144
        control.updateInput();
×
UNCOV
145

×
UNCOV
146
        expect(controller.range[0]).not.equals(prevPanel.position);
×
UNCOV
147
        expect(controller.range[0]).equals(camera.range.min);
×
148
        expect(controller.range[1]).not.equals(nextPanel.position);
149
        expect(controller.range[1]).equals(camera.range.max);
150
      });
151

1✔
152
      it("should always set Axes's circular to false even if circular mode is enabled", async () => {
UNCOV
153
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, { moveType: MOVE_TYPE.STRICT, circular: true });
×
UNCOV
154
        const controller = flicking.control.controller;
×
UNCOV
155

×
156
        expect(flicking.circularEnabled).to.be.true;
157
        expect(controller.controlParams.circular).to.be.false;
158
      });
UNCOV
159

×
UNCOV
160
      it("should extend controller range larger than camera range when circular=true", async () => {
×
UNCOV
161
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, { moveType: MOVE_TYPE.STRICT, circular: true });
×
UNCOV
162
        const panels = flicking.panels;
×
163
        const controller = flicking.control.controller;
UNCOV
164
        const cameraRangeDiff = flicking.camera.rangeDiff;
×
UNCOV
165

×
UNCOV
166
        expect(flicking.circularEnabled).to.be.true;
×
UNCOV
167

×
168
        await flicking.moveTo(0, 0);
UNCOV
169

×
UNCOV
170
        expect(controller.range[0]).to.equal(panels[2].position - cameraRangeDiff);
×
UNCOV
171
        expect(controller.range[1]).to.equal(panels[1].position);
×
UNCOV
172

×
173
        await flicking.moveTo(2, 0);
UNCOV
174

×
UNCOV
175
        expect(controller.range[0]).to.equal(panels[1].position);
×
UNCOV
176
        expect(controller.range[1]).to.equal(panels[0].position + cameraRangeDiff);
×
UNCOV
177
      });
×
178

UNCOV
179
      it("should update based on nearest panel when updated during animation", async () => {
×
UNCOV
180
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, { moveType: MOVE_TYPE.STRICT });
×
UNCOV
181
        const panels = flicking.panels;
×
UNCOV
182
        const camera = flicking.camera;
×
183
        const control = flicking.control;
184
        const controller = control.controller;
185

186
        // This should reach panel 1
1✔
187
        void simulate(flicking.element, { deltaX: -99999, duration: 1000 }, 800);
UNCOV
188

×
UNCOV
189
        expect(flicking.animating).to.be.true;
×
UNCOV
190
        expect(camera.findNearestAnchor(camera.position).panel.index).to.equal(1);
×
191

UNCOV
192
        control.updateInput();
×
UNCOV
193

×
UNCOV
194
        expect(controller.range[0]).to.equal(panels[0].position);
×
UNCOV
195
        expect(controller.range[1]).to.equal(panels[2].position);
×
UNCOV
196
      });
×
UNCOV
197
    });
×
UNCOV
198

×
UNCOV
199
    describe("moveToPosition", () => {
×
UNCOV
200
      it("should be rejected returning FlickingError with code NOT_ATTACHED_TO_FLICKING if control is not initialized", async () => {
×
UNCOV
201
        const control = new StrictControl();
×
UNCOV
202

×
UNCOV
203
        expect(() => control.moveToPosition(0, 0))
×
204
          .to.throw(FlickingError)
205
          .with.property("code", ERROR.CODE.NOT_ATTACHED_TO_FLICKING);
206
      });
207

1✔
208
      it("should be rejected returning FlickingError with code POSITION_NOT_REACHABLE when there are no panels exist", async () => {
UNCOV
209
        const flicking = await createFlicking(El.viewport().add(El.camera()), { moveType: MOVE_TYPE.STRICT });
×
UNCOV
210
        const control = flicking.control;
×
UNCOV
211

×
212
        const err = await control.moveToPosition(500, 0).catch(e => e);
UNCOV
213
        expect(err)
×
UNCOV
214
          .to.be.instanceOf(FlickingError)
×
UNCOV
215
          .with.property("code", ERROR.CODE.POSITION_NOT_REACHABLE);
×
UNCOV
216
      });
×
UNCOV
217

×
218
      it("should not exceed movable index range", async () => {
219
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, { moveType: MOVE_TYPE.STRICT });
220
        const control = flicking.control;
221

1✔
222
        await control.moveToPosition(99999999, 0);
UNCOV
223

×
UNCOV
224
        expect(flicking.index).to.equal(1);
×
UNCOV
225

×
226
        const flicking2 = await createFlicking(El.DEFAULT_HORIZONTAL, { moveType: [MOVE_TYPE.STRICT, { count: 2 }] });
UNCOV
227
        const control2 = flicking2.control;
×
UNCOV
228

×
UNCOV
229
        await control2.moveToPosition(99999999, 0);
×
UNCOV
230

×
UNCOV
231
        expect(flicking2.index).to.equal(2);
×
UNCOV
232
      });
×
233

UNCOV
234
      it("should not exceed movable index range, when circular is true", async () => {
×
UNCOV
235
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, { moveType: MOVE_TYPE.STRICT, circular: true });
×
UNCOV
236
        const control = flicking.control;
×
UNCOV
237

×
238
        await control.moveToPosition(-99999999, 0);
UNCOV
239

×
UNCOV
240
        expect(flicking.index).to.equal(2);
×
UNCOV
241

×
UNCOV
242
        const flicking2 = await createFlicking(El.DEFAULT_HORIZONTAL, { moveType: [MOVE_TYPE.STRICT, { count: 2 }], circular: true });
×
243
        const control2 = flicking2.control;
244

245
        await control2.moveToPosition(-99999999, 0);
246

1✔
247
        expect(flicking2.index).to.equal(1);
UNCOV
248
      });
×
UNCOV
249

×
UNCOV
250
      it("should move to first anchor when going out of bound when bound=true", async () => {
×
251
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, { moveType: MOVE_TYPE.STRICT, bound: true });
UNCOV
252
        const control = flicking.control;
×
UNCOV
253
        const firstAnchor = flicking.camera.anchorPoints[0];
×
UNCOV
254

×
UNCOV
255
        await control.moveToPosition(-99999999, 0);
×
UNCOV
256

×
257
        expect(flicking.index).to.equal(firstAnchor.panel.index);
UNCOV
258
      });
×
UNCOV
259

×
UNCOV
260
      it("should move to last anchor when going out of bound when bound=true", async () => {
×
UNCOV
261
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, { moveType: MOVE_TYPE.STRICT, bound: true, defaultIndex: 2 });
×
UNCOV
262
        const control = flicking.control;
×
UNCOV
263
        const lastAnchor = flicking.camera.anchorPoints[flicking.camera.anchorPoints.length - 1];
×
UNCOV
264

×
265
        await control.moveToPosition(99999999, 0);
266

267
        expect(flicking.index).to.equal(lastAnchor.panel.index);
268
      });
269

1✔
270
      it("Should move to the nearest panel from the camera, when animation is interrupted by user input", async () => {
1✔
271
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, { moveType: MOVE_TYPE.STRICT });
UNCOV
272

×
UNCOV
273
        const control = flicking.control;
×
UNCOV
274
        const camera = flicking.camera;
×
275
        const moveSpy = sinon.spy(control, "moveToPanel");
276

UNCOV
277
        // Suppress animation interrupt error
×
278
        const promise = flicking.moveTo(1, 1500).catch(error => error);
279
        tick(100);
280
        const position = camera.position;
1✔
281
        // Simulate interrupt
UNCOV
282
        await simulate(flicking.element, { deltaX: 0, duration: 100 }, 1000);
×
UNCOV
283
        tick(1000);
×
UNCOV
284
        await promise;
×
285

UNCOV
286
        expect(moveSpy.calledTwice).to.be.true;
×
UNCOV
287
        expect(control.activePanel.index).to.equal(camera.findNearestAnchor(position).panel.index);
×
UNCOV
288
      });
×
289

UNCOV
290
      it("should determine the next panel based on the target panel of the willChange event", async () => {
×
UNCOV
291
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, { moveType: MOVE_TYPE.STRICT, circular: true, duration: 5000, threshold: 0 });
×
292

293
        await simulate(flicking.element, { deltaX: 900, duration: 100 }, 1000);
UNCOV
294
        tick(500);
×
295
        await simulate(flicking.element, { deltaX: 900, duration: 100 }, 1000);
296
        tick(500);
297
        await simulate(flicking.element, { deltaX: 900, duration: 100 }, 1000);
298
        tick(5000);
1✔
299

UNCOV
300
        expect(flicking.index).to.equal(0);
×
UNCOV
301
      });
×
UNCOV
302
    });
×
303
  });
UNCOV
304
});
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc