This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
Add TS to useWindowScroll #1590
Merged
Merged
Changes from all commits
Commits
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
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,65 @@ | ||
import Vue from 'vue' | ||
import { ref } from '@nuxtjs/composition-api' | ||
import { render } from '@testing-library/vue' | ||
|
||
import { useWindowScroll } from '~/composables/use-window-scroll' | ||
|
||
const getMockWindow = <T>(props: T) => | ||
({ | ||
addEventListener: jest.fn(), | ||
removeEventListener: jest.fn(), | ||
...props, | ||
} as unknown as typeof window) | ||
|
||
const UseWindowScrollTestContainer = Vue.component( | ||
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. Very interesting setup! 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. I think it might be possible to create a "generic" component for this to test composables, but I'm not sure 😅 |
||
'UseWindowScrollTestContainer', | ||
{ | ||
props: ['initX', 'initY', 'throttleMs'], | ||
setup(props) { | ||
return useWindowScroll({ | ||
window: getMockWindow({ | ||
scrollX: props.initX, | ||
scrollY: props.initY, | ||
}), | ||
throttleMs: props.throttleMs as number | undefined, | ||
}) | ||
}, | ||
template: '<div>x={{x}} y={{y}} isScrolled={{isScrolled}}</div>', | ||
} | ||
) | ||
|
||
describe('useWindowScroll', () => { | ||
it('should return [0, 0] and false when no window', () => { | ||
expect(useWindowScroll({})).toMatchObject({ | ||
x: ref(0), | ||
y: ref(0), | ||
isScrolled: ref(false), | ||
}) | ||
}) | ||
|
||
it("should return the window's scroll position and not scrolled when y == 0", () => { | ||
const { container } = render(UseWindowScrollTestContainer, { | ||
props: { | ||
initX: 10, | ||
initY: 0, | ||
}, | ||
}) | ||
|
||
expect(container.firstChild?.textContent).toEqual( | ||
'x=10 y=0 isScrolled=false' | ||
) | ||
}) | ||
|
||
it("should return the window's scroll position and scrolled when y != 0", () => { | ||
const { container } = render(UseWindowScrollTestContainer, { | ||
props: { | ||
initX: 31, | ||
initY: 1, | ||
}, | ||
}) | ||
|
||
expect(container.firstChild?.textContent).toEqual( | ||
'x=31 y=1 isScrolled=true' | ||
) | ||
}) | ||
}) |
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.
I don't understand why this object is frozen.
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.
To encode the immutability of the structure? Changing the structure of this object is meaningless so why allow it? Maybe I am over thinking this though.