Skip to content

Commit

Permalink
fix: remove axes instance on destory (#26)
Browse files Browse the repository at this point in the history
* fix: remove axes instance

* test: test init again after destroy

* chore: remove only
  • Loading branch information
daybrush authored Dec 2, 2022
1 parent 976d2aa commit f7e7769
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/conveyer/src/Conveyer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const conveyer = new Conveyer(".items");
@ReactiveSubscribe
class Conveyer extends Component<ConveyerEvents> {
protected _scrollAreaElement: HTMLElement;
protected _axes: Axes;
protected _axes: Axes | null = null;
protected _items: ConveyerItem[] = [];
protected _size = 0;
protected _scrollSize = 0;
Expand Down Expand Up @@ -275,7 +275,7 @@ class Conveyer extends Component<ConveyerEvents> {
* @param - Duration to scroll by that position. <ko>해당 위치만큼 스크롤하는 시간</ko>
*/
public scrollBy(pos: number, duration = 0) {
this._axes.setBy({ scroll: -pos }, duration);
this._axes!.setBy({ scroll: -pos }, duration);
}
/**
* Scroll to the given position.
Expand All @@ -284,7 +284,7 @@ class Conveyer extends Component<ConveyerEvents> {
* @param - Duration to scroll to that position. <ko>해당 위치로 스크롤하는 시간</ko>
*/
public scrollTo(pos: number, duration = 0) {
this._axes.setBy({ scroll: this._pos - pos }, duration);
this._axes!.setBy({ scroll: this._pos - pos }, duration);
}
/**
* Set the items directly to the Conveyer.
Expand Down Expand Up @@ -457,6 +457,7 @@ class Conveyer extends Component<ConveyerEvents> {
this._scrollAreaElement?.removeEventListener("scroll", this._onScroll);
window.removeEventListener("resize", this.update);
this.off();
this._axes = null;
}
private _refreshScroll() {
const horizontal = this._options.horizontal;
Expand Down
20 changes: 20 additions & 0 deletions packages/conveyer/test/unit/Conveyer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,26 @@ describe("test Conveyer", () => {
// Then
expect(clickSpy.callCount).to.be.equals(1);
});
it("should check if it works even if you init again after destroy", async () => {
// Given
const items = document.querySelector<HTMLElement>(".items")!;

conveyer = new Conveyer(items);

// When
conveyer.destroy();
conveyer.init();

await dispatchDrag(
items,
{ left: 0, top: 0 },
{ left: -100, top: 0 },
{ duration: 100, interval: 50 }
);

await waitEvent(conveyer, "finishScroll");
expect(items.scrollLeft).to.be.at.least(100);
});
});
describe("Methods", () => {
it("should check if scrollLeft is changed when scrollTo is called", async () => {
Expand Down

0 comments on commit f7e7769

Please sign in to comment.