-
Notifications
You must be signed in to change notification settings - Fork 665
Scheduler - Appointments Refactoring - Custom Templates #33159
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
328 changes: 328 additions & 0 deletions
328
packages/devextreme/js/__internal/scheduler/__tests__/appointments_new.test.ts
This file contains hidden or 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,328 @@ | ||
| import { | ||
| afterEach, beforeEach, describe, expect, it, jest, | ||
| } from '@jest/globals'; | ||
| import type { dxElementWrapper } from '@js/core/renderer'; | ||
| import $ from '@js/core/renderer'; | ||
| import type { Properties } from '@js/ui/scheduler'; | ||
|
|
||
| import { createScheduler as baseCreateScheduler } from './__mock__/create_scheduler'; | ||
| import { setupSchedulerTestEnvironment } from './__mock__/m_mock_scheduler'; | ||
| import type { SchedulerModel } from './__mock__/model/scheduler'; | ||
|
|
||
| const createScheduler = (config: Properties) => baseCreateScheduler({ | ||
| ...config, | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| _newAppointments: true, | ||
| }); | ||
|
|
||
| describe('New Appointments', () => { | ||
| beforeEach(() => { | ||
| setupSchedulerTestEnvironment(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| const $scheduler = $('.dx-scheduler'); | ||
| // @ts-expect-error | ||
| $scheduler.dxScheduler('dispose'); | ||
| document.body.innerHTML = ''; | ||
| }); | ||
|
|
||
| describe('Templates', () => { | ||
| describe.each([ | ||
| 'appointmentTemplate', | ||
| 'appointmentCollectorTemplate', | ||
| ])('%s common', (templateName) => { | ||
| const config = { | ||
| dataSource: [ | ||
| { text: 'Appointment 1', startDate: new Date(2015, 1, 9, 8), endDate: new Date(2015, 1, 9, 9) }, | ||
| { text: 'Appointment 2', startDate: new Date(2015, 1, 9, 8), endDate: new Date(2015, 1, 9, 9) }, | ||
| { text: 'Appointment 3', startDate: new Date(2015, 1, 9, 8), endDate: new Date(2015, 1, 9, 9) }, | ||
| ], | ||
| maxAppointmentsPerCell: 2, | ||
| currentView: 'day', | ||
| currentDate: new Date(2015, 1, 9, 8), | ||
| }; | ||
|
|
||
| const appointmentCollectorTemplate = ( | ||
| data: any, | ||
| container: HTMLElement, | ||
| ): dxElementWrapper => $(container).text('Custom collector'); | ||
|
|
||
| const appointmentTemplate = ( | ||
| data: any, | ||
| index: number, | ||
| container: HTMLElement, | ||
| ): dxElementWrapper => $(container).text('Custom appointment'); | ||
|
|
||
| const templateFunction = templateName === 'appointmentCollectorTemplate' | ||
| ? appointmentCollectorTemplate | ||
| : appointmentTemplate; | ||
|
|
||
| const isTemplateApplied = (POM: SchedulerModel): boolean => { | ||
| if (templateName === 'appointmentCollectorTemplate') { | ||
| return $(POM.getCollectorButton()).text() === 'Custom collector'; | ||
| } | ||
|
|
||
| return $(POM.getAppointments()[0].element).text() === 'Custom appointment'; | ||
| }; | ||
|
|
||
| it('should apply custom template', async () => { | ||
| const { POM } = await createScheduler({ | ||
| ...config, | ||
| [templateName]: templateFunction, | ||
| }); | ||
|
|
||
| expect(isTemplateApplied(POM)).toBe(true); | ||
| }); | ||
|
|
||
| it('should apply custom template after .option() change', async () => { | ||
| const { POM, scheduler } = await createScheduler(config); | ||
|
|
||
| scheduler.option(templateName, templateFunction); | ||
| await new Promise(process.nextTick); | ||
|
|
||
| expect(isTemplateApplied(POM)).toBe(true); | ||
| }); | ||
|
|
||
| it('should apply default template if current view does not have it', async () => { | ||
| const defaultTemplate = jest.fn(); | ||
|
|
||
| const { POM } = await createScheduler({ | ||
| ...config, | ||
| views: [{ type: 'day' }], | ||
| [templateName]: defaultTemplate, | ||
| }); | ||
|
|
||
| expect(defaultTemplate).toHaveBeenCalled(); | ||
| expect(isTemplateApplied(POM)).toBe(false); | ||
| }); | ||
|
|
||
| it('should apply default template after .option() change with default value', async () => { | ||
| const defaultValue = templateName === 'appointmentCollectorTemplate' | ||
| ? 'appointmentCollector' | ||
| : 'appointment'; | ||
|
|
||
| const { POM, scheduler } = await createScheduler({ | ||
| ...config, | ||
| [templateName]: templateFunction, | ||
| }); | ||
|
|
||
| scheduler.option(templateName, defaultValue); | ||
| await new Promise(process.nextTick); | ||
|
|
||
| expect(isTemplateApplied(POM)).toBe(false); | ||
|
|
||
| if (templateName === 'appointmentTemplate') { | ||
| const appointment = POM.getAppointments()[0]; | ||
| expect(appointment.getText()).toBe('Appointment 1'); | ||
| } else { | ||
| const collectorButton = POM.getCollectorButton(); | ||
| expect(collectorButton.textContent).toBe('1'); | ||
| } | ||
| }); | ||
|
|
||
| it('should apply current view\'s template', async () => { | ||
| const defaultTemplate = jest.fn(); | ||
|
|
||
| const { POM } = await createScheduler({ | ||
| ...config, | ||
| views: [{ | ||
| type: 'day', | ||
| [templateName]: templateFunction, | ||
| }], | ||
| [templateName]: defaultTemplate, | ||
| }); | ||
|
|
||
| expect(defaultTemplate).not.toHaveBeenCalled(); | ||
|
|
||
| expect(isTemplateApplied(POM)).toBe(true); | ||
| }); | ||
|
|
||
| it('should apply current view\'s template after .option() change', async () => { | ||
| const { POM, scheduler } = await createScheduler({ | ||
| ...config, | ||
| views: [{ | ||
| type: 'day', | ||
| [templateName]: templateFunction, | ||
| }], | ||
| }); | ||
|
|
||
| const defaultTemplate = jest.fn(); | ||
|
|
||
| scheduler.option(templateName, defaultTemplate); | ||
| await new Promise(process.nextTick); | ||
|
|
||
| expect(defaultTemplate).not.toHaveBeenCalled(); | ||
| expect(isTemplateApplied(POM)).toBe(true); | ||
| }); | ||
|
|
||
| it('should apply current view\'s template after current view was changed', async () => { | ||
| const defaultTemplate = jest.fn(); | ||
|
|
||
| const { POM, scheduler } = await createScheduler({ | ||
| ...config, | ||
| views: [ | ||
| { type: 'workWeek', [templateName]: defaultTemplate }, | ||
| { type: 'day', [templateName]: templateFunction }, | ||
| ], | ||
| currentView: 'workWeek', | ||
| }); | ||
|
|
||
| defaultTemplate.mockClear(); | ||
|
|
||
| scheduler.option('currentView', 'day'); | ||
| await new Promise(process.nextTick); | ||
|
|
||
| expect(defaultTemplate).not.toHaveBeenCalled(); | ||
| expect(isTemplateApplied(POM)).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('AppointmentTemplate', () => { | ||
| it('should call template function with correct parameters', async () => { | ||
| const appointmentTemplate = jest.fn(); | ||
|
|
||
| const appointmentData1 = { | ||
| text: 'Appointment 1', | ||
| startDate: new Date(2015, 1, 9, 8), | ||
| endDate: new Date(2015, 1, 9, 9), | ||
| }; | ||
| const appointmentData2 = { | ||
| text: 'Appointment 2', | ||
| startDate: new Date(2015, 1, 9, 10), | ||
| endDate: new Date(2015, 1, 9, 11), | ||
| }; | ||
|
|
||
| await createScheduler({ | ||
| dataSource: [appointmentData1, appointmentData2], | ||
| currentView: 'day', | ||
| currentDate: new Date(2015, 1, 9, 8), | ||
| appointmentTemplate, | ||
| }); | ||
|
|
||
| expect(appointmentTemplate).toHaveBeenCalledTimes(2); | ||
| expect(appointmentTemplate.mock.calls[0]).toHaveLength(3); | ||
| expect(appointmentTemplate.mock.calls[0]).toEqual([ | ||
| expect.objectContaining({ | ||
| appointmentData: appointmentData1, | ||
| targetedAppointmentData: expect.objectContaining({ | ||
| ...appointmentData1, | ||
| displayStartDate: appointmentData1.startDate, | ||
| displayEndDate: appointmentData1.endDate, | ||
| }), | ||
| }), | ||
| 0, | ||
| expect.any(HTMLElement), | ||
| ]); | ||
|
|
||
| const container1 = appointmentTemplate.mock.calls[0][2] as HTMLElement; | ||
| expect(container1.classList.contains('dx-scheduler-appointment-content')).toBe(true); | ||
| expect(container1.innerHTML).toBe(''); | ||
|
|
||
| expect(appointmentTemplate.mock.calls[1]).toHaveLength(3); | ||
| expect(appointmentTemplate.mock.calls[1]).toEqual([ | ||
| expect.objectContaining({ | ||
| appointmentData: appointmentData2, | ||
| targetedAppointmentData: expect.objectContaining({ | ||
| ...appointmentData2, | ||
| displayStartDate: appointmentData2.startDate, | ||
| displayEndDate: appointmentData2.endDate, | ||
| }), | ||
| }), | ||
| 1, | ||
| expect.any(HTMLElement), | ||
| ]); | ||
|
|
||
| const container2 = appointmentTemplate.mock.calls[1][2] as HTMLElement; | ||
| expect(container2.classList.contains('dx-scheduler-appointment-content')).toBe(true); | ||
| expect(container2.innerHTML).toBe(''); | ||
| }); | ||
| }); | ||
|
|
||
| describe('AppointmentCollectorTemplate', () => { | ||
| it('should call template function with correct parameters', async () => { | ||
| const appointmentCollectorTemplate = jest.fn(); | ||
|
|
||
| await createScheduler({ | ||
| dataSource: [ | ||
| { | ||
| text: 'Appointment 1', | ||
| startDate: new Date(2015, 1, 9, 8), | ||
| endDate: new Date(2015, 1, 9, 9), | ||
| }, | ||
| { | ||
| text: 'Appointment 2', | ||
| startDate: new Date(2015, 1, 9, 8), | ||
| endDate: new Date(2015, 1, 9, 9), | ||
| }, | ||
| { | ||
| text: 'Appointment 3', | ||
| startDate: new Date(2015, 1, 9, 8), | ||
| endDate: new Date(2015, 1, 9, 9), | ||
| }, | ||
| ], | ||
| currentView: 'day', | ||
| currentDate: new Date(2015, 1, 9, 8), | ||
| maxAppointmentsPerCell: 2, | ||
| appointmentCollectorTemplate, | ||
| }); | ||
|
|
||
| expect(appointmentCollectorTemplate).toHaveBeenCalledTimes(1); | ||
| expect(appointmentCollectorTemplate.mock.calls[0]).toHaveLength(2); | ||
| expect(appointmentCollectorTemplate.mock.calls[0]).toEqual([ | ||
| expect.objectContaining({ | ||
| appointmentCount: 1, | ||
| isCompact: true, | ||
| items: [ | ||
| expect.objectContaining({ | ||
| text: 'Appointment 3', | ||
| }), | ||
| ], | ||
| }), | ||
| expect.any(HTMLElement), | ||
| ]); | ||
|
|
||
| const container = appointmentCollectorTemplate.mock.calls[0][1] as HTMLElement; | ||
| expect(container.classList.contains('dx-button-content')).toBe(true); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('onAppointmentRendered', () => { | ||
| it('should call onAppointmentRendered callback', async () => { | ||
| const onAppointmentRendered = jest.fn(); | ||
|
|
||
| await createScheduler({ | ||
| dataSource: [{ | ||
| text: 'Appointment 1', | ||
| startDate: new Date(2015, 1, 9, 8), | ||
| endDate: new Date(2015, 1, 9, 9), | ||
| }], | ||
| currentView: 'day', | ||
| currentDate: new Date(2015, 1, 9, 8), | ||
| onAppointmentRendered, | ||
| }); | ||
|
|
||
| expect(onAppointmentRendered).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should call onAppointmentRendered after .option() change', async () => { | ||
| const { scheduler } = await createScheduler({ | ||
| dataSource: [{ | ||
| text: 'Appointment 1', | ||
| startDate: new Date(2015, 1, 9, 8), | ||
| endDate: new Date(2015, 1, 9, 9), | ||
| }], | ||
| currentView: 'day', | ||
| currentDate: new Date(2015, 1, 9, 8), | ||
| }); | ||
|
|
||
| const onAppointmentRendered = jest.fn(); | ||
| scheduler.option('onAppointmentRendered', onAppointmentRendered); | ||
| scheduler.repaint(); | ||
| await new Promise(process.nextTick); | ||
|
|
||
| expect(onAppointmentRendered).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or 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 hidden or 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
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.
In this test we want to be sure that appointment collector is rendering properly and is having only one item. How about adding an additional assertion that checks that in items there is only one item?
Something like
expect(data.items).toHaveLength(1);
expect(data.items[0]).toEqual(expect.objectContaining({
text: 'Appointment 3',
}));
We have appointmentCount: 1 check here, but if we have a bug and in items being passed more than 1 appt we won't detect it in test
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.
update test to:
now it should pass only if there is one item in the array