-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
272 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type Action = (node: HTMLElement, params?: any) => ActionLifecycleHandlers; | ||
export type ActionLifecycleHandlers = { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
update?: (params: any) => void, | ||
destroy? : () => void, | ||
} | ||
|
||
export const getElements = () => [ | ||
global.document.createElement('div'), | ||
global.document.createElement('div'), | ||
]; | ||
|
||
export const mountWithAction | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
= (node: HTMLElement, action: Action, params?: any) => action(node, params); | ||
|
||
export const unmount = (_: HTMLElement, lifecycle: ActionLifecycleHandlers) => { | ||
if (lifecycle.destroy) { | ||
lifecycle.destroy(); | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { createPopperActions } from '../src/index'; | ||
import { getElements, mountWithAction, unmount } from './helpers'; | ||
|
||
test('instance should init if and only if ref and content is mounted', () => { | ||
// Arrange | ||
const [refDiv, contentDiv] = getElements(); | ||
const [refAction, contentAction, getInstance] = createPopperActions(); | ||
|
||
// Act and Assert | ||
expect(getInstance()).toBeNull(); | ||
mountWithAction(refDiv, refAction); | ||
|
||
expect(getInstance()).toBeNull(); | ||
mountWithAction(contentDiv, contentAction); | ||
|
||
expect(getInstance()).not.toBeNull(); | ||
}); | ||
|
||
test('instance should deinit if ref is unmounted', () => { | ||
// Arrange | ||
const [refDiv, contentDiv] = getElements(); | ||
const [refAction, contentAction, getInstance] = createPopperActions(); | ||
mountWithAction(contentDiv, contentAction); | ||
const refLifecycle = mountWithAction(refDiv, refAction); | ||
|
||
// Act | ||
unmount(refDiv, refLifecycle); | ||
|
||
// Assert | ||
expect(getInstance()).toBeNull(); | ||
}); | ||
|
||
test('instance should deinit if content is unmounted', () => { | ||
// Arrange | ||
const [refDiv, contentDiv] = getElements(); | ||
const [refAction, contentAction, getInstance] = createPopperActions(); | ||
mountWithAction(refDiv, refAction); | ||
const contentLifecycle = mountWithAction(contentDiv, contentAction); | ||
|
||
// Act | ||
unmount(contentDiv, contentLifecycle); | ||
|
||
// Assert | ||
expect(getInstance()).toBeNull(); | ||
}); | ||
|
||
test('instance should deinit if ref and content is unmounted', () => { | ||
// Arrange | ||
const [refDiv, contentDiv] = getElements(); | ||
const [refAction, contentAction, getInstance] = createPopperActions(); | ||
const refLifecycle = mountWithAction(refDiv, refAction); | ||
const contentLifecycle = mountWithAction(contentDiv, contentAction); | ||
|
||
// Act | ||
unmount(refDiv, refLifecycle); | ||
unmount(contentDiv, contentLifecycle); | ||
|
||
// Assert | ||
expect(getInstance()).toBeNull(); | ||
}); | ||
|
Oops, something went wrong.