Skip to content

Commit

Permalink
feat: exposes gotoPeriod method to top level (#212)
Browse files Browse the repository at this point in the history
* feat: exposes gotoPeriod method to top level

* test: add unit test for new public method goToPeriod

* test: fix bad test naming

---------

Co-authored-by: Tom Österlund <[email protected]>
  • Loading branch information
MarcelR1998 and tomosterlund authored Nov 30, 2023
1 parent 170d759 commit 18b091c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/Qalendar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</Transition>

<AppHeader
ref="appHeader"
:key="wasInitialized + mode"
:config="config"
:mode="mode"
Expand Down Expand Up @@ -335,7 +336,11 @@ export default defineComponent({
handleDateWasClicked(payload: string) {
this.$emit('day-was-clicked', payload); // TODO: remove with v4. day-was-clicked is deprecated
this.$emit('date-was-clicked', payload);
}
},
goToPeriod(direction: 'previous' | 'next') {
(this.$refs.appHeader as typeof AppHeader).goToPeriod(direction);
},
},
});
</script>
Expand Down
6 changes: 3 additions & 3 deletions src/components/header/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
<FontAwesomeIcon
class="calendar-header__chevron-arrow calendar-header__chevron-arrow-left"
:icon="icons.chevronLeft"
@click="goToPeriod($event, 'previous')"
@click="goToPeriod('previous')"
/>

<FontAwesomeIcon
class="calendar-header__chevron-arrow calendar-header__chevron-arrow-right"
:icon="icons.chevronRight"
@click="goToPeriod($event, 'next')"
@click="goToPeriod('next')"
/>
</div>

Expand Down Expand Up @@ -188,7 +188,7 @@ export default defineComponent({
this.$emit('updated-period', value);
},
goToPeriod(event: MouseEvent, direction: 'previous' | 'next') {
goToPeriod(direction: 'previous' | 'next') {
(this.$refs.periodSelect as typeof DatePicker).goToPeriod(direction);
},
}
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/components/Qalendar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,4 +480,15 @@ describe('Qalendar.vue', () => {
month.vm.$emit('updated-period', { start: new Date(), end: new Date(), selectedDate: new Date() })
expect(wrapper.vm.mode).toBe('day')
})

it('should invoke the goToPeriod method in the header', () => {
const wrapper = mount(Qalendar)
const header = wrapper.findComponent({ name: 'AppHeader' })
const goToPeriodSpy = vi.spyOn(header.vm, 'goToPeriod')
const expectedDirection = 'forward'

wrapper.vm.goToPeriod(expectedDirection)

expect(goToPeriodSpy).toHaveBeenCalledWith(expectedDirection)
})
})

0 comments on commit 18b091c

Please sign in to comment.