-
Notifications
You must be signed in to change notification settings - Fork 75
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
fix: fix regression causing open/close events from emitting in proper order #9560
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fccd7e6
fix: fix regression causing open/close events from emitting in proper…
jcfranco 2f832ff
tidy up
jcfranco e5fb3d0
call start/end hooks for 0-length duration
jcfranco 0fd768d
call start/end hooks for fallback timeout
jcfranco a43fd94
increase coverage
jcfranco bd4ffad
move spec helpers to tests folder
jcfranco 2ec1285
fix imports
jcfranco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
68 changes: 68 additions & 0 deletions
68
packages/calcite-components/src/utils/openCloseComponent.spec.ts
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,68 @@ | ||
import * as openCloseComponent from "./openCloseComponent"; | ||
import { createTransitionEventDispatcher, TransitionEventDispatcher } from "./spec-helpers/transitionEvents"; | ||
|
||
const { onToggleOpenCloseComponent } = openCloseComponent; | ||
|
||
describe("openCloseComponent", () => { | ||
describe("toggleOpenCloseComponent", () => { | ||
let dispatchTransitionEvent: TransitionEventDispatcher; | ||
|
||
beforeEach(() => { | ||
jest.spyOn(openCloseComponent, "internalReadTask").mockImplementation((task) => task(1337)); | ||
dispatchTransitionEvent = createTransitionEventDispatcher(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it("emits beforeOpen/beforeClose events when the transition starts and open/close events when the transition is done", async () => { | ||
const transitionEl = window.document.createElement("div"); | ||
const testProp = "opacity"; | ||
const testDuration = "0.5s"; | ||
const testTransition = `${testProp} ${testDuration} ease 0s`; | ||
|
||
transitionEl.style.transition = testTransition; | ||
|
||
// need to mock due to JSDOM issue with getComputedStyle - https://github.com/jsdom/jsdom/issues/3090 | ||
window.getComputedStyle = jest.fn().mockReturnValue({ | ||
transition: testTransition, | ||
transitionDuration: testDuration, | ||
transitionProperty: testProp, | ||
}); | ||
window.document.body.append(transitionEl); | ||
|
||
const emittedEvents: string[] = []; | ||
const fakeOpenCloseComponent = { | ||
el: document.createElement("div"), | ||
open: true, | ||
openTransitionProp: "opacity", | ||
transitionEl, | ||
onBeforeOpen: jest.fn(() => emittedEvents.push("beforeOpen")), | ||
onOpen: jest.fn(() => emittedEvents.push("open")), | ||
onBeforeClose: jest.fn(() => emittedEvents.push("beforeClose")), | ||
onClose: jest.fn(() => emittedEvents.push("close")), | ||
}; | ||
|
||
onToggleOpenCloseComponent(fakeOpenCloseComponent); | ||
expect(emittedEvents).toEqual([]); | ||
|
||
dispatchTransitionEvent(transitionEl, "transitionstart", fakeOpenCloseComponent.openTransitionProp); | ||
expect(emittedEvents).toEqual(["beforeOpen"]); | ||
|
||
dispatchTransitionEvent(transitionEl, "transitionend", fakeOpenCloseComponent.openTransitionProp); | ||
expect(emittedEvents).toEqual(["beforeOpen", "open"]); | ||
|
||
fakeOpenCloseComponent.open = false; | ||
|
||
onToggleOpenCloseComponent(fakeOpenCloseComponent); | ||
expect(emittedEvents).toEqual(["beforeOpen", "open"]); | ||
|
||
dispatchTransitionEvent(transitionEl, "transitionstart", fakeOpenCloseComponent.openTransitionProp); | ||
expect(emittedEvents).toEqual(["beforeOpen", "open", "beforeClose"]); | ||
|
||
dispatchTransitionEvent(transitionEl, "transitionend", fakeOpenCloseComponent.openTransitionProp); | ||
expect(emittedEvents).toEqual(["beforeOpen", "open", "beforeClose", "close"]); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,6 +1,11 @@ | ||
import { readTask } from "@stencil/core"; | ||
import { whenTransitionDone } from "./dom"; | ||
|
||
/** | ||
* Exported for testing purposes only | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🥼 🔬 |
||
*/ | ||
export const internalReadTask = readTask; | ||
|
||
/** | ||
* Defines interface for components with open/close public emitter. | ||
* All implementations of this interface must handle the following events: `beforeOpen`, `open`, `beforeClose`, `close`. | ||
|
@@ -81,20 +86,28 @@ function isOpen(component: OpenCloseComponent): boolean { | |
* @param component - OpenCloseComponent uses `open` prop to emit (before)open/close. | ||
*/ | ||
export function onToggleOpenCloseComponent(component: OpenCloseComponent): void { | ||
readTask(async (): Promise<void> => { | ||
internalReadTask((): void => { | ||
if (!component.transitionEl) { | ||
return; | ||
} | ||
|
||
await whenTransitionDone(component.transitionEl, component.openTransitionProp); | ||
|
||
if (isOpen(component)) { | ||
component.onBeforeOpen(); | ||
component.onOpen(); | ||
return; | ||
} | ||
|
||
component.onBeforeClose(); | ||
component.onClose(); | ||
whenTransitionDone( | ||
component.transitionEl, | ||
component.openTransitionProp, | ||
() => { | ||
if (isOpen(component)) { | ||
component.onBeforeOpen(); | ||
} else { | ||
component.onBeforeClose(); | ||
} | ||
}, | ||
() => { | ||
if (isOpen(component)) { | ||
component.onOpen(); | ||
} else { | ||
component.onClose(); | ||
} | ||
}, | ||
); | ||
}); | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/calcite-components/src/utils/spec-helpers/animationEvents.ts
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,33 @@ | ||
import { JSDOM } from "jsdom"; | ||
|
||
export interface AnimationEventDispatcher { | ||
(element: HTMLElement, type: "animationstart" | "animationend", animationName: string): void; | ||
} | ||
|
||
/** | ||
* Must be called in a `beforeEach` block to create a animation event dispatcher. | ||
*/ | ||
export function createAnimationEventDispatcher(): AnimationEventDispatcher { | ||
// we clobber Stencil's custom Mock document implementation | ||
const { window: win } = new JSDOM(); | ||
|
||
// eslint-disable-next-line no-global-assign -- overriding to make window references use JSDOM (which is a subset, hence the type cast) | ||
window = win as any as Window & typeof globalThis; | ||
|
||
// we define AnimationEvent since JSDOM doesn't support it yet - | ||
class AnimationEvent extends window.Event { | ||
elapsedTime: number; | ||
|
||
animationName: string; | ||
|
||
constructor(type: string, eventInitDict: EventInit & Partial<{ elapsedTime: number; animationName: string }>) { | ||
super(type, eventInitDict); | ||
this.elapsedTime = eventInitDict.elapsedTime; | ||
this.animationName = eventInitDict.animationName; | ||
} | ||
} | ||
|
||
return (element: HTMLElement, type: "animationstart" | "animationend", animationName: string): void => { | ||
element.dispatchEvent(new AnimationEvent(type, { animationName })); | ||
}; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍