Skip to content

Commit

Permalink
test: Truncated class
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-berlin committed Feb 1, 2024
1 parent 49bf30b commit c258f1d
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tests/unit/truncated.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Truncated } from "../../src/lib/truncated.ts";
import { test, beforeEach, vi } from "vitest";
import { JSDOM } from "jsdom";

let instance: Truncated;
let callback: any;

beforeEach(() => {
const dom = new JSDOM();
global.document = dom.window.document;
global.customElements = dom.window.customElements;
global.HTMLElement = dom.window.HTMLElement;
global.ResizeObserver = class {
constructor(cb: any) {
callback = cb;
}
observe(entry: any) {
callback([entry]);
}
unobserve() {}
disconnect() {}
};

// Define the Truncated custom element
class Truncated extends HTMLElement {
constructor() {
super();
// Your custom element's constructor logic here
}
}

// Register the Truncated custom element
global.customElements.define("x-truncated", Truncated);

instance = new Truncated();
instance.toggleTruncated = vi.fn();
});

test("triggers toggleTruncated with true when overflowing", () => {
const entry = { target: { clientWidth: 100 } };

// Create a new ResizeObserver and observe the entry
const resizeObserver = new global.ResizeObserver((entries) => {
// Call instance.toggleTruncated inside the callback
if (entries[0].target.clientWidth < 200) {
instance.toggleTruncated(true);
}
});

resizeObserver.observe(entry);

// Simulate a change in the entry's size
entry.target.clientWidth = 50;

// Call the callback with the updated entry
callback([entry]);

// Check that instance.toggleTruncated was called with the argument [true]
expect(instance.toggleTruncated).toHaveBeenCalledWith(true);
});

0 comments on commit c258f1d

Please sign in to comment.