Skip to content

Commit

Permalink
Add IntersectionObserver mock
Browse files Browse the repository at this point in the history
  • Loading branch information
jhildenbiddle committed Apr 15, 2024
1 parent 9f9572f commit 7c1a308
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
43 changes: 38 additions & 5 deletions test/config/jest.setup-tests.js
Expand Up @@ -21,8 +21,32 @@ const sideEffects = {
},
};

class IntersectionObserver {
constructor() {}

root = null;
rootMargin = '';
thresholds = [];

disconnect() {
return null;
}

observe() {
return null;
}

takeRecords() {
return [];
}

unobserve() {
return null;
}
}

// Lifecycle Hooks
// -----------------------------------------------------------------------------
// =============================================================================
beforeAll(async () => {
// Spy addEventListener
['document', 'window'].forEach(obj => {
Expand All @@ -44,13 +68,16 @@ beforeAll(async () => {
});
});

// Reset JSDOM. This attempts to remove side effects from tests, however it does
// not reset all changes made to globals like the window and document
// objects. Tests requiring a full JSDOM reset should be stored in separate
// files, which is only way to do a complete JSDOM reset with Jest.
beforeEach(async () => {
const rootElm = document.documentElement;

// Reset JSDOM
// -----------------------------------------------------------------------------
// This attempts to remove side effects from tests, however it does not reset
// all changes made to globals like the window and document objects. Tests
// requiring a full JSDOM reset should be stored in separate files, which is
// only way to do a complete JSDOM reset with Jest.

// Remove attributes on root element
[...rootElm.attributes].forEach(attr => rootElm.removeAttribute(attr.name));

Expand Down Expand Up @@ -79,6 +106,12 @@ beforeEach(async () => {

// Restore base elements
rootElm.innerHTML = '<head></head><body></body>';

// Mock IntersectionObserver
// -----------------------------------------------------------------------------
[global, window].forEach(
obj => (obj.IntersectionObserver = IntersectionObserver)
);
});

afterEach(async () => {
Expand Down
1 change: 0 additions & 1 deletion test/integration/docsify.test.js
Expand Up @@ -11,7 +11,6 @@ describe('Docsify', function () {
expect(vm).toBeInstanceOf(Object);
expect(vm.constructor.name).toBe('Docsify');
expect(vm.$fetch).toBeInstanceOf(Function);
expect(vm.$resetEvents).toBeInstanceOf(Function);
expect(vm.route).toBeInstanceOf(Object);
});

Expand Down

0 comments on commit 7c1a308

Please sign in to comment.