• 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

14.35
/test/unit/control/SnapControl.spec.ts
1
import FlickingError from "~/core/FlickingError";
1✔
2
import SnapControl from "~/control/SnapControl";
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("SnapControl", () => {
1!
10
  describe("Methods", () => {
×
11
    describe("moveToPosition", () => {
12
      it("should be rejected returning FlickingError with code NOT_ATTACHED_TO_FLICKING if control is not initialized", async () => {
×
13
        const control = new SnapControl();
14

1✔
15
        expect(() => control.moveToPosition(0, 0))
1!
16
          .to.throw(FlickingError)
×
17
          .with.property("code", ERROR.CODE.NOT_ATTACHED_TO_FLICKING);
×
18
      });
×
19

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

×
UNCOV
24
        expect(control).to.be.an.instanceOf(SnapControl);
×
UNCOV
25

×
UNCOV
26
        try {
×
UNCOV
27
          await control.moveToPosition(500, 0);
×
28
          expect(true).to.be.false;
29
        } catch (err) {
30
          expect(err)
1✔
UNCOV
31
            .to.be.instanceOf(FlickingError)
×
UNCOV
32
            .with.property("code", ERROR.CODE.POSITION_NOT_REACHABLE);
×
UNCOV
33
        }
×
34
      });
UNCOV
35

×
UNCOV
36
      it("should return to current panel if position delta is smaller than threshold", async () => {
×
UNCOV
37
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, {
×
UNCOV
38
          moveType: MOVE_TYPE.SNAP,
×
UNCOV
39
          threshold: 40
×
UNCOV
40
        });
×
UNCOV
41
        const control = flicking.control;
×
42

×
43
        const promise = control.moveToPosition(control.activePanel.position + 39, 500);
×
44
        tick(1000);
UNCOV
45
        await promise;
×
UNCOV
46

×
UNCOV
47
        expect(control.activePanel).to.equal(flicking.getPanel(0));
×
UNCOV
48
      });
×
UNCOV
49

×
UNCOV
50
      it("should move to adjacent panel if position delta is bigger than threshold", async () => {
×
51
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, {
UNCOV
52
          moveType: MOVE_TYPE.SNAP,
×
UNCOV
53
          threshold: 40
×
UNCOV
54
        });
×
55
        const control = flicking.control;
56

57
        const promise = control.moveToPosition(control.activePanel.position + 40, 500);
1✔
58
        tick(1000);
3!
59
        await promise;
60

1✔
61
        expect(control.activePanel).to.equal(flicking.getPanel(1));
1✔
62
      });
1✔
63

1✔
64
      it("should clamp to camera range even if it's further outside of camera range", async () => {
1✔
65
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, {
1✔
66
          moveType: MOVE_TYPE.SNAP
1✔
67
        });
1✔
68
        const control = flicking.control;
1✔
69

1✔
70
        const promise = control.moveToPosition(999999999999999, 500);
1✔
71
        tick(1000);
UNCOV
72
        await promise;
×
UNCOV
73

×
UNCOV
74
        expect(control.activePanel).to.equal(flicking.getPanel(2));
×
75
      });
76

UNCOV
77
      it("should not be rejected when user interrupted while animating with user input", async () => {
×
78
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, {
79
          moveType: MOVE_TYPE.SNAP
80
        });
1✔
81

UNCOV
82
        const control = flicking.control;
×
UNCOV
83
        const changedSpy = sinon.spy();
×
UNCOV
84
        const moveToSpy = sinon.spy(control, "moveToPosition");
×
85
        flicking.on("changed", changedSpy);
UNCOV
86

×
UNCOV
87
        await simulate(flicking.element, { deltaX: -300, duration: 100 }, 150);
×
UNCOV
88
        await simulate(flicking.element, { deltaX: -300, duration: 100 });
×
UNCOV
89

×
90
        const err = await (moveToSpy.firstCall.returnValue.catch(error => error));
UNCOV
91

×
UNCOV
92
        expect(err).to.be.undefined;
×
93
        expect(changedSpy.calledOnce).to.be.true;
UNCOV
94
      });
×
95

×
96
      it("should move to the below panel when dragged with no acceleration", async () => {
×
97
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, {
UNCOV
98
          moveType: MOVE_TYPE.SNAP
×
UNCOV
99
        });
×
100

101
        const control = flicking.control;
UNCOV
102

×
UNCOV
103
        // Drag with enough duration to prevent acceleration
×
104
        await simulate(flicking.element, { deltaX: -2000, duration: 9999 }, 15000);
105

106
        expect(control.activePanel.index).to.equal(flicking.getPanel(2).index);
107
      });
1✔
108

UNCOV
109
      it("should move to the below panel when dragged with no acceleration, when count is applied", async () => {
×
UNCOV
110
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, {
×
UNCOV
111
          moveType: [MOVE_TYPE.SNAP, { count: 1 }]
×
112
        });
113

114
        const control = flicking.control;
115

UNCOV
116
        // Drag with enough duration to prevent acceleration
×
UNCOV
117
        await simulate(flicking.element, { deltaX: -2000, duration: 9999 }, 15000);
×
UNCOV
118

×
UNCOV
119
        expect(control.activePanel.index).to.equal(flicking.getPanel(2).index);
×
UNCOV
120
      });
×
121

UNCOV
122
      it("should move to the correct adjacent panel", async () => {
×
UNCOV
123
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, {
×
UNCOV
124
          moveType: MOVE_TYPE.SNAP,
×
125
          threshold: 40
126
        });
127

128
        const control = flicking.control;
1✔
129

UNCOV
130
        // Drag with enough duration to prevent acceleration
×
UNCOV
131
        await simulate(flicking.element, { deltaX: -40, duration: 9999 }, 15000);
×
UNCOV
132

×
133
        expect(control.activePanel.index).to.equal(flicking.getPanel(1).index);
134
      });
135

136
      it("should move to the correct adjacent panel, when circular is applied", async () => {
UNCOV
137
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, {
×
UNCOV
138
          moveType: MOVE_TYPE.SNAP,
×
UNCOV
139
          circular: true,
×
UNCOV
140
          threshold: 40
×
UNCOV
141
        });
×
142

UNCOV
143
        const control = flicking.control;
×
UNCOV
144

×
UNCOV
145
        // Drag with enough duration to prevent acceleration
×
146
        await simulate(flicking.element, { deltaX: -40, duration: 9999 }, 15000);
147

148
        expect(control.activePanel.index).to.equal(flicking.getPanel(1).index);
149
      });
1✔
150

UNCOV
151
      it("should move to the correct adjacent panel, when circular is applied and moving from index 0 to last", async () => {
×
UNCOV
152
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, {
×
UNCOV
153
          moveType: MOVE_TYPE.SNAP,
×
154
          circular: true,
155
          threshold: 40
156
        });
UNCOV
157

×
UNCOV
158
        const control = flicking.control;
×
UNCOV
159

×
UNCOV
160
        // Drag with enough duration to prevent acceleration
×
UNCOV
161
        await simulate(flicking.element, { deltaX: 40, duration: 9999 }, 15000);
×
162

UNCOV
163
        expect(control.activePanel.index).to.equal(flicking.getPanel(2).index);
×
UNCOV
164
      });
×
UNCOV
165

×
166
      it("should move to the correct adjacent panel, when circular is applied and moving from last index to 0", async () => {
167
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, {
168
          moveType: MOVE_TYPE.SNAP,
169
          circular: true,
1✔
170
          defaultIndex: 2,
UNCOV
171
          threshold: 40
×
UNCOV
172
        });
×
UNCOV
173

×
174
        const control = flicking.control;
175

176
        // Drag with enough duration to prevent acceleration
UNCOV
177
        await simulate(flicking.element, { deltaX: -40, duration: 9999 }, 15000);
×
UNCOV
178

×
UNCOV
179
        expect(control.activePanel.index).to.equal(flicking.getPanel(0).index);
×
UNCOV
180
      });
×
UNCOV
181

×
UNCOV
182
      it("should move to the panel with index diff by the given count", async () => {
×
183
        const flicking = await createFlicking(
UNCOV
184
          El.viewport("1000px", "300px").add(
×
UNCOV
185
            El.camera().add(
×
186
              El.panel("100%", "100%"),
UNCOV
187
              El.panel("100%", "100%"),
×
UNCOV
188
              El.panel("100%", "100%"),
×
189
              El.panel("100%", "100%"),
UNCOV
190
              El.panel("100%", "100%")
×
UNCOV
191
            )
×
UNCOV
192
          ),
×
UNCOV
193
          { moveType: [MOVE_TYPE.SNAP, { count: 1 }] }
×
194
        );
195

196
        // Start from the index 1
197
        void simulate(flicking.element, { deltaX: -1000, duration: 9999 });
1✔
198
        tick(9000);
UNCOV
199

×
UNCOV
200
        await simulate(flicking.element, { deltaX: -400, duration: 50 });
×
UNCOV
201

×
202
        expect(flicking.index).to.equal(2);
203
      });
204

UNCOV
205
      it("should move to the panel with index diff by the given count, in circular case", async () => {
×
UNCOV
206
        const flicking = await createFlicking(
×
207
          El.viewport("1000px", "300px").add(
UNCOV
208
            El.camera().add(
×
209
              El.panel("100%", "100%"),
210
              El.panel("100%", "100%"),
UNCOV
211
              El.panel("100%", "100%"),
×
UNCOV
212
              El.panel("100%", "100%"),
×
UNCOV
213
              El.panel("100%", "100%")
×
214
            )
215
          ),
216
          { moveType: [MOVE_TYPE.SNAP, { count: 1 }], circular: true }
217
        );
1✔
218

UNCOV
219
        // Start from the index 0
×
UNCOV
220
        await simulate(flicking.element, { deltaX: 400, duration: 50 });
×
UNCOV
221

×
222
        expect(flicking.index).to.equal(4);
223
      });
224

UNCOV
225
      it("should restore to the current panel with the nearest position", async () => {
×
UNCOV
226
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, {
×
227
          moveType: MOVE_TYPE.SNAP,
UNCOV
228
          circular: true,
×
229
          align: "prev"
230
        });
UNCOV
231

×
UNCOV
232
        const control = flicking.control;
×
UNCOV
233
        const controller = control.controller;
×
234
        const animateSpy = sinon.spy(controller, "animateTo");
235

236
        // Drag with enough duration to prevent acceleration
237
        await simulate(flicking.element, { deltaX: 20, duration: 10000 }, 15000);
1✔
238

UNCOV
239
        const position = animateSpy.firstCall.args[0];
×
UNCOV
240

×
UNCOV
241
        expect(position).to.be.greaterThan(0);
×
242
        expect(position).to.equal(flicking.panels[0].position + flicking.camera.rangeDiff);
243
      });
244

245
      it("should move to the adjacent panel with the nearest position, from first to last panel", async () => {
UNCOV
246
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, {
×
UNCOV
247
          moveType: MOVE_TYPE.SNAP,
×
248
          circular: true,
UNCOV
249
          align: "prev"
×
250
        });
251

UNCOV
252
        const control = flicking.control;
×
UNCOV
253
        const controller = control.controller;
×
UNCOV
254
        const animateSpy = sinon.spy(controller, "animateTo");
×
255

256
        // Drag with enough duration to prevent acceleration
257
        await simulate(flicking.element, { deltaX: 60, duration: 10000 }, 15000);
258

1✔
259
        const position = animateSpy.firstCall.args[0];
UNCOV
260

×
UNCOV
261
        expect(position).to.be.greaterThan(0);
×
UNCOV
262
        expect(position).to.equal(flicking.panels[flicking.panelCount - 1].position);
×
263
      });
264

265
      it("should move to the adjacent panel with the nearest position, from last to first panel", async () => {
266
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, {
267
          moveType: MOVE_TYPE.SNAP,
UNCOV
268
          defaultIndex: 2,
×
UNCOV
269
          circular: true,
×
270
          align: "next"
UNCOV
271
        });
×
272

273
        const control = flicking.control;
UNCOV
274
        const controller = control.controller;
×
UNCOV
275
        const animateSpy = sinon.spy(controller, "animateTo");
×
UNCOV
276

×
277
        // Drag with enough duration to prevent acceleration
278
        await simulate(flicking.element, { deltaX: -60, duration: 10000 }, 15000);
279

280
        const position = animateSpy.firstCall.args[0];
1✔
281

UNCOV
282
        expect(position).to.be.lessThan(flicking.panels[flicking.panelCount - 1].position);
×
UNCOV
283
        expect(position).to.equal(flicking.panels[0].position);
×
UNCOV
284
      });
×
285

286
      it("should move to the nearest panel from the camera, when animation is interrupted by user input", async () => {
287
        const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, {
288
          moveType: MOVE_TYPE.SNAP,
289
          align: "prev",
UNCOV
290
        });
×
UNCOV
291

×
292
        const control = flicking.control;
UNCOV
293
        const camera = flicking.camera;
×
294
        const moveSpy = sinon.spy(control, "moveToPanel");
295

UNCOV
296
        // Suppress animation interrupt error
×
UNCOV
297
        const promise = flicking.moveTo(2, 1500).catch(error => error);
×
UNCOV
298
        tick(500);
×
299
        const position = camera.position;
300
        // Simulate interrupt
301
        await simulate(flicking.element, { deltaX: 0, duration: 100 }, 1000);
302
        tick(1000);
1✔
303
        await promise;
UNCOV
304

×
UNCOV
305
        expect(moveSpy.calledTwice).to.be.true;
×
UNCOV
306
        expect(control.activePanel.index).to.equal(camera.findNearestAnchor(position).panel.index);
×
307
      });
308

309
      it("should use the closest anchor even if there is no anchor for the active panel.", async () => {
310
        const flicking = await createFlicking(
311
          El.viewport().add(
312
            El.camera()
UNCOV
313
              .add(El.panel(`33%`))
×
UNCOV
314
              .add(El.panel(`33%`))
×
315
              .add(El.panel(`33%`))
UNCOV
316
              .add(El.panel(`33%`))
×
317
          ), {
318
            moveType: MOVE_TYPE.SNAP,
UNCOV
319
            bound: true // when bound is true, there is no anchor for first panel
×
UNCOV
320
          }
×
UNCOV
321
        );
×
322

323
        // Set active panel to first panel
324
        void flicking.moveTo(0, 0);
325
        await simulate(flicking.element, { deltaX: -1000, duration: 100 });
1✔
326

UNCOV
327
        const status = flicking.getStatus();
×
UNCOV
328

×
UNCOV
329
        expect(status.index).equals(2);
×
330
      });
UNCOV
331
    });
×
332
  });
UNCOV
333
});
×
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