|
| 1 | +import { DomToModelContext, ModelToDomContext, RoleFormat } from 'roosterjs-content-model-types'; |
| 2 | +import { roleFormatHandler } from '../../../lib/formatHandlers/common/roleFormatHandler'; |
| 3 | +import { createDomToModelContext } from '../../../lib/domToModel/context/createDomToModelContext'; |
| 4 | +import { createModelToDomContext } from '../../../lib/modelToDom/context/createModelToDomContext'; |
| 5 | + |
| 6 | +describe('roleFormatHandler.parse', () => { |
| 7 | + let div: HTMLElement; |
| 8 | + let format: RoleFormat; |
| 9 | + let context: DomToModelContext; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + div = document.createElement('div'); |
| 13 | + format = {}; |
| 14 | + context = createDomToModelContext(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('No role', () => { |
| 18 | + roleFormatHandler.parse(format, div, context, {}); |
| 19 | + expect(format).toEqual({}); |
| 20 | + }); |
| 21 | + |
| 22 | + it('has role', () => { |
| 23 | + div.setAttribute('role', 'button'); |
| 24 | + roleFormatHandler.parse(format, div, context, {}); |
| 25 | + expect(format).toEqual({ |
| 26 | + role: 'button', |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + it('has role with different value', () => { |
| 31 | + div.setAttribute('role', 'tabpanel'); |
| 32 | + roleFormatHandler.parse(format, div, context, {}); |
| 33 | + expect(format).toEqual({ |
| 34 | + role: 'tabpanel', |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('has empty role', () => { |
| 39 | + div.setAttribute('role', ''); |
| 40 | + roleFormatHandler.parse(format, div, context, {}); |
| 41 | + expect(format).toEqual({}); |
| 42 | + }); |
| 43 | + |
| 44 | + it('table with role="table"', () => { |
| 45 | + const table = document.createElement('table'); |
| 46 | + table.setAttribute('role', 'table'); |
| 47 | + roleFormatHandler.parse(format, table, context, {}); |
| 48 | + expect(format).toEqual({ |
| 49 | + role: 'table', |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('table with role="grid"', () => { |
| 54 | + const table = document.createElement('table'); |
| 55 | + table.setAttribute('role', 'grid'); |
| 56 | + roleFormatHandler.parse(format, table, context, {}); |
| 57 | + expect(format).toEqual({ |
| 58 | + role: 'grid', |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it('table with role="presentation"', () => { |
| 63 | + const table = document.createElement('table'); |
| 64 | + table.setAttribute('role', 'presentation'); |
| 65 | + roleFormatHandler.parse(format, table, context, {}); |
| 66 | + expect(format).toEqual({ |
| 67 | + role: 'presentation', |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('table with role="treegrid"', () => { |
| 72 | + const table = document.createElement('table'); |
| 73 | + table.setAttribute('role', 'treegrid'); |
| 74 | + roleFormatHandler.parse(format, table, context, {}); |
| 75 | + expect(format).toEqual({ |
| 76 | + role: 'treegrid', |
| 77 | + }); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +describe('roleFormatHandler.apply', () => { |
| 82 | + let div: HTMLElement; |
| 83 | + let format: RoleFormat; |
| 84 | + let context: ModelToDomContext; |
| 85 | + |
| 86 | + beforeEach(() => { |
| 87 | + div = document.createElement('div'); |
| 88 | + format = {}; |
| 89 | + context = createModelToDomContext(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('No role', () => { |
| 93 | + roleFormatHandler.apply(format, div, context); |
| 94 | + expect(div.outerHTML).toBe('<div></div>'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('Has role', () => { |
| 98 | + format.role = 'button'; |
| 99 | + roleFormatHandler.apply(format, div, context); |
| 100 | + expect(div.outerHTML).toBe('<div role="button"></div>'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('Has role with different value', () => { |
| 104 | + format.role = 'tabpanel'; |
| 105 | + roleFormatHandler.apply(format, div, context); |
| 106 | + expect(div.outerHTML).toBe('<div role="tabpanel"></div>'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('Role applied to different element types', () => { |
| 110 | + format.role = 'navigation'; |
| 111 | + const nav = document.createElement('nav'); |
| 112 | + roleFormatHandler.apply(format, nav, context); |
| 113 | + expect(nav.outerHTML).toBe('<nav role="navigation"></nav>'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('Apply role="table" to table element', () => { |
| 117 | + format.role = 'table'; |
| 118 | + const table = document.createElement('table'); |
| 119 | + roleFormatHandler.apply(format, table, context); |
| 120 | + expect(table.outerHTML).toBe('<table role="table"></table>'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('Apply role="grid" to table element', () => { |
| 124 | + format.role = 'grid'; |
| 125 | + const table = document.createElement('table'); |
| 126 | + roleFormatHandler.apply(format, table, context); |
| 127 | + expect(table.outerHTML).toBe('<table role="grid"></table>'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('Apply role="presentation" to table element', () => { |
| 131 | + format.role = 'presentation'; |
| 132 | + const table = document.createElement('table'); |
| 133 | + roleFormatHandler.apply(format, table, context); |
| 134 | + expect(table.outerHTML).toBe('<table role="presentation"></table>'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('Apply role="treegrid" to table element', () => { |
| 138 | + format.role = 'treegrid'; |
| 139 | + const table = document.createElement('table'); |
| 140 | + roleFormatHandler.apply(format, table, context); |
| 141 | + expect(table.outerHTML).toBe('<table role="treegrid"></table>'); |
| 142 | + }); |
| 143 | + |
| 144 | + it('Apply role to table cell elements', () => { |
| 145 | + format.role = 'gridcell'; |
| 146 | + const td = document.createElement('td'); |
| 147 | + roleFormatHandler.apply(format, td, context); |
| 148 | + expect(td.outerHTML).toBe('<td role="gridcell"></td>'); |
| 149 | + }); |
| 150 | + |
| 151 | + it('Apply role to table header elements', () => { |
| 152 | + format.role = 'columnheader'; |
| 153 | + const th = document.createElement('th'); |
| 154 | + roleFormatHandler.apply(format, th, context); |
| 155 | + expect(th.outerHTML).toBe('<th role="columnheader"></th>'); |
| 156 | + }); |
| 157 | + |
| 158 | + it('Apply role to table row elements', () => { |
| 159 | + format.role = 'row'; |
| 160 | + const tr = document.createElement('tr'); |
| 161 | + roleFormatHandler.apply(format, tr, context); |
| 162 | + expect(tr.outerHTML).toBe('<tr role="row"></tr>'); |
| 163 | + }); |
| 164 | +}); |
0 commit comments