Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converts ResizeMirror test to typescript #590

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smooth-coins-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/draggable': patch
---

Fixes VSCode search to exclude generated files/folders
5 changes: 5 additions & 0 deletions .changeset/sour-ravens-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/draggable': patch
---

Converts ResizeMirror test to typescript
10 changes: 5 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
"coverage": true
},
"search.exclude": {
"**/node_modules": true,
".rollup.cache": true,
"coverage": true,
"build": true,
"docs": true
"node_modules/**/*": true,
".rollup.cache/**/*": true,
"coverage/**/*": true,
"build/**/*": true,
"docs/**/*": true
},
"[typescript]": {
"editor.formatOnSave": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import {
DRAG_DELAY,
drag,
} from 'helper';
import {FixMeAny} from 'shared/types';

import {Draggable} from '../../..';
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
import Draggable from '../../../Draggable';
/* eslint-enable @typescript-eslint/ban-ts-comment */
import ResizeMirror from '..';

const sampleMarkup = `
Expand All @@ -36,17 +40,17 @@ describe('ResizeMirror', () => {
height: smallerDraggableDimensions.height * 2,
};

let sandbox;
let containers;
let draggable;
let draggables;
let smallerDraggable;
let largerDraggable;
let sandbox: HTMLDivElement;
let containers: HTMLElement[];
let draggable: FixMeAny;
let draggables: HTMLElement[];
let smallerDraggable: HTMLElement;
let largerDraggable: HTMLElement;

beforeEach(() => {
sandbox = createSandbox(sampleMarkup);
containers = sandbox.querySelectorAll('.Container');
draggables = sandbox.querySelectorAll('li');
containers = [...sandbox.querySelectorAll<HTMLElement>('.Container')];
draggables = [...sandbox.querySelectorAll<HTMLElement>('li')];

smallerDraggable = draggables[0];
largerDraggable = draggables[1];
Expand All @@ -71,7 +75,7 @@ describe('ResizeMirror', () => {
waitForDragDelay();
await waitForPromisesToResolve();

const mirror = document.querySelector('.draggable-mirror');
const mirror = document.querySelector<HTMLElement>('.draggable-mirror')!;

expect(mirror.style).toMatchObject({
width: `${smallerDraggableDimensions.width}px`,
Expand Down Expand Up @@ -104,7 +108,7 @@ describe('ResizeMirror', () => {
waitForDragDelay();
await waitForPromisesToResolve();

const mirror = document.querySelector('.draggable-mirror');
const mirror = document.querySelector<HTMLElement>('.draggable-mirror')!;

moveMouse(largerDraggable);
waitForRequestAnimationFrame();
Expand All @@ -119,7 +123,7 @@ describe('ResizeMirror', () => {
waitForDragDelay();
await waitForPromisesToResolve();

const mirror = document.querySelector('.draggable-mirror');
const mirror = document.querySelector<HTMLElement>('.draggable-mirror')!;

const mockedAppendChild = withMockedAppendChild(() => {
moveMouse(smallerDraggable);
Expand All @@ -145,19 +149,28 @@ describe('ResizeMirror', () => {
});
});

function mockDimensions(element, {width = 0, height = 0}) {
function mockDimensions(element: HTMLElement, {width = 0, height = 0}) {
Object.assign(element.style, {
width: `${width}px`,
height: `${height}px`,
});

element.getBoundingClientRect = () => ({
const properties = {
width,
height,
top: 0,
left: 0,
right: width,
bottom: height,
x: 0,
y: 0,
};

element.getBoundingClientRect = () => ({
...properties,
toJSON() {
return {...properties};
},
});

return element;
Expand All @@ -168,13 +181,16 @@ function waitForNextRequestAnimationFrame() {
waitForRequestAnimationFrame();
}

function withMockedAppendChild(callback) {
function withMockedAppendChild(callback: () => void) {
const mock = jest.fn();
const appendChild = Node.prototype.appendChild;
Node.prototype.appendChild = function (...args) {
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
Node.prototype.appendChild = function (this: Node, ...args) {
mock(...args);
return appendChild.call(this, ...args);
};
/* eslint-enable @typescript-eslint/ban-ts-comment */
callback();
Node.prototype.appendChild = appendChild;
return mock;
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion test/helpers/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
interface Options {
from: HTMLElement;
to: HTMLElement;
sensor: 'mouse' | 'touch' | 'drag';
sensor?: 'mouse' | 'touch' | 'drag';
}

export function drag({from, to, sensor = 'mouse'}: Options) {
Expand Down