|
| 1 | +import $ from 'jquery'; |
| 2 | +import 'ui/gantt'; |
| 3 | +import { Consts } from '../../../helpers/ganttHelpers.js'; |
| 4 | +import devices from '__internal/core/m_devices'; |
| 5 | +const { test } = QUnit; |
| 6 | + |
| 7 | +const moduleConfig = { |
| 8 | + _customTasks: [ |
| 9 | + { 'id': 1, 'parentId': 0, 'title': 'Software Development', 'start': new Date('2019-02-21'), 'end': new Date('2019-03-26'), 'progress': 0 }, |
| 10 | + { 'id': 2, 'parentId': 1, 'title': 'Milestone', 'start': new Date('2019-02-21'), 'end': new Date('2019-02-21'), 'progress': 0 }, |
| 11 | + ], |
| 12 | + beforeEach: function() { |
| 13 | + this.createInstance = (settings) => { |
| 14 | + this.instance = this.$element.dxGantt({ |
| 15 | + tasks: { dataSource: this._customTasks }, |
| 16 | + taskTitlePosition: 'outside', |
| 17 | + ...settings |
| 18 | + }).dxGantt('instance'); |
| 19 | + }; |
| 20 | + |
| 21 | + this.$element = $('#gantt'); |
| 22 | + this.clock = sinon.useFakeTimers(); |
| 23 | + }, |
| 24 | + afterEach: function() { |
| 25 | + this.clock.restore(); |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +QUnit.module('Milestone', moduleConfig, () => { |
| 30 | + test('default', function(assert) { |
| 31 | + this.createInstance(); |
| 32 | + this.clock.tick(10); |
| 33 | + const milestoneWrapper = this.$element.find(Consts.MILESTONE_WRAPPER_SELECTOR); |
| 34 | + const children = milestoneWrapper.children(); |
| 35 | + assert.equal(children.length, 2, 'milestone wrapper has 2 children'); |
| 36 | + assert.true(children.eq(0).hasClass(Consts.TASK_TITLE_SELECTOR), 'first child is task title'); |
| 37 | + assert.true(children.eq(1).hasClass(Consts.MILESTONE_SELECTOR), 'second child is milestone'); |
| 38 | + }); |
| 39 | + |
| 40 | + test('custom template, default html', function(assert) { |
| 41 | + const customHtml = $(document.createElement('div')); |
| 42 | + const customTaskFunction = (item, container) => { |
| 43 | + if(item.isMilestone) { |
| 44 | + const title = $(item.taskHTML[0].cloneNode(true)); |
| 45 | + const milestone = $(item.taskHTML[1].cloneNode(true)); |
| 46 | + assert.true(title.hasClass(Consts.TASK_TITLE_SELECTOR), 'taskHTML has task title'); |
| 47 | + assert.true(milestone.hasClass(Consts.MILESTONE_SELECTOR), 'taskHTML has milestone'); |
| 48 | + assert.equal(item.taskHTML.length, 2, 'taskHTML has 2 children'); |
| 49 | + |
| 50 | + title.appendTo(customHtml); |
| 51 | + milestone.appendTo(customHtml); |
| 52 | + } |
| 53 | + $(item.taskHTML).appendTo(container); |
| 54 | + }; |
| 55 | + const options = { taskContentTemplate: customTaskFunction }; |
| 56 | + this.createInstance(options); |
| 57 | + this.clock.tick(10); |
| 58 | + const milestoneWrapper = this.$element.find(Consts.MILESTONE_WRAPPER_SELECTOR); |
| 59 | + assert.equal(milestoneWrapper.length, 1, 'milestone wrapper exists'); |
| 60 | + assert.equal(milestoneWrapper.children().length, 1, 'milestone wrapper has 1 child'); |
| 61 | + assert.equal(milestoneWrapper.html(), customHtml[0].outerHTML, 'milestone html matches custom html'); |
| 62 | + }); |
| 63 | + |
| 64 | + test('custom template, custom html', function(assert) { |
| 65 | + const customHtml = $(document.createElement('div')).text('milestone'); |
| 66 | + const customTaskFunction = (item, container) => { |
| 67 | + if(item.isMilestone) { |
| 68 | + return customHtml.text(); |
| 69 | + } |
| 70 | + }; |
| 71 | + const options = { taskContentTemplate: customTaskFunction }; |
| 72 | + this.createInstance(options); |
| 73 | + this.clock.tick(10); |
| 74 | + const milestoneWrapper = this.$element.find(Consts.MILESTONE_WRAPPER_SELECTOR); |
| 75 | + assert.equal(milestoneWrapper.length, 1, 'milestone wrapper exists'); |
| 76 | + assert.equal(milestoneWrapper.html(), customHtml[0].outerHTML, 'milestone html matches custom html'); |
| 77 | + }); |
| 78 | + |
| 79 | + test('custom template, custom html + task html', function(assert) { |
| 80 | + const customHtml = $(document.createElement('div')).text('milestone'); |
| 81 | + const customTaskFunction = (item, container) => { |
| 82 | + if(item.isMilestone) { |
| 83 | + $(item.taskHTML[1].cloneNode(true)).appendTo(customHtml); |
| 84 | + return customHtml.html(); |
| 85 | + } |
| 86 | + }; |
| 87 | + const options = { taskContentTemplate: customTaskFunction }; |
| 88 | + this.createInstance(options); |
| 89 | + this.clock.tick(10); |
| 90 | + const milestoneWrapper = this.$element.find(Consts.MILESTONE_WRAPPER_SELECTOR); |
| 91 | + assert.equal(milestoneWrapper.length, 1, 'milestone wrapper exists'); |
| 92 | + assert.equal(milestoneWrapper.html(), customHtml[0].outerHTML, 'milestone html matches custom html'); |
| 93 | + }); |
| 94 | + |
| 95 | + test('task title inside', function(assert) { |
| 96 | + const options = { |
| 97 | + taskTitlePosition: 'inside', |
| 98 | + taskContentTemplate: (item, container) => $(item.taskHTML).appendTo(container) |
| 99 | + }; |
| 100 | + this.createInstance(options); |
| 101 | + this.clock.tick(10); |
| 102 | + const milestoneWrapper = this.$element.find(Consts.MILESTONE_WRAPPER_SELECTOR); |
| 103 | + assert.equal(milestoneWrapper.length, 1, 'milestone wrapper exists'); |
| 104 | + const children = milestoneWrapper.children().eq(0).children(); |
| 105 | + assert.equal(children.length, 1, 'milestone has one child'); |
| 106 | + assert.true(children.eq(0).hasClass(Consts.MILESTONE_SELECTOR), 'child is milestone'); |
| 107 | + assert.false(children.eq(0).hasClass(Consts.TASK_TITLE_SELECTOR), 'child is not task title'); |
| 108 | + }); |
| 109 | + |
| 110 | + QUnit.skipInShadowDomMode('position is correctly calculated', function(assert) { |
| 111 | + const { platform } = devices.real(); |
| 112 | + if(['android', 'ios'].includes(platform)) { |
| 113 | + assert.expect(0); |
| 114 | + return; |
| 115 | + } |
| 116 | + const customTasks = [ |
| 117 | + { 'id': 1, 'parentId': 0, 'title': 'Software Development', 'start': new Date('2019-02-21'), 'end': new Date('2019-03-26'), 'progress': 0 }, |
| 118 | + { 'id': 2, 'parentId': 1, 'title': 'Milestone 1', 'start': new Date('2019-02-21'), 'end': new Date('2019-02-21'), 'progress': 0 }, |
| 119 | + { 'id': 3, 'parentId': 2, 'title': 'Milestone 2', 'start': new Date('2019-02-21'), 'end': new Date('2019-02-21'), 'progress': 0 }, |
| 120 | + ]; |
| 121 | + const customTaskFunction = (item, container) => { |
| 122 | + if(item.isMilestone) { |
| 123 | + const style = item.taskData.id === 2 ? 'width: 10px; height: 10px;' : 'width: 100px; height: 50px;'; |
| 124 | + return $(document.createElement('div')).attr('style', style); |
| 125 | + } |
| 126 | + }; |
| 127 | + const options = { |
| 128 | + tasks: { dataSource: customTasks }, |
| 129 | + taskContentTemplate: customTaskFunction |
| 130 | + }; |
| 131 | + this.createInstance(options); |
| 132 | + this.clock.tick(10); |
| 133 | + const milestoneWrapper = this.$element.find(Consts.MILESTONE_WRAPPER_SELECTOR); |
| 134 | + const firstLeft = parseFloat(milestoneWrapper.eq(0).css('left')); |
| 135 | + const secondLeft = parseFloat(milestoneWrapper.eq(1).css('left')); |
| 136 | + assert.equal(firstLeft - secondLeft, 45, 'left position is different'); |
| 137 | + }); |
| 138 | +}); |
0 commit comments