MCPcopy Create free account
hub / github.com/testing-library/user-event / createDataTransferStub

Function createDataTransferStub

src/utils/dataTransfer/DataTransfer.ts:98–160  ·  view source on GitHub ↗
(window: Window & typeof globalThis)

Source from the content-addressed store, hash-verified

96}
97
98function createDataTransferStub(window: Window & typeof globalThis) {
99 return new (class DataTransferStub implements DataTransfer {
100 getData(format: string) {
101 const type = normalizeFormat(format)
102 const match =
103 this.items.find(getTypeMatcher(type, true)) ??
104 this.items.find(getTypeMatcher(type, false))
105
106 let text = ''
107 match?.getAsString(t => {
108 text = t
109 })
110
111 return text
112 }
113
114 setData(format: string, data: string) {
115 const type = normalizeFormat(format)
116 const matchIndex = this.items.findIndex(getTypeMatcher(type, true))
117
118 const item = new DataTransferItemStub(data, type) as DataTransferItem
119 if (matchIndex >= 0) {
120 this.items.splice(matchIndex, 1, item)
121 } else {
122 this.items.push(item)
123 }
124 }
125
126 clearData(format?: string) {
127 if (format) {
128 const type = normalizeFormat(format)
129 const matchIndex = this.items.findIndex(getTypeMatcher(type, true))
130
131 if (matchIndex >= 0) {
132 this.items.remove(matchIndex)
133 }
134 } else {
135 this.items.clear()
136 }
137 }
138
139 dropEffect: DataTransfer['dropEffect'] = 'none'
140 effectAllowed: DataTransfer['effectAllowed'] = 'uninitialized'
141
142 readonly items = new DataTransferItemListStub()
143 readonly files = createFileList(window, [])
144
145 get types() {
146 const t = []
147 if (this.files.length) {
148 t.push('Files')
149 }
150 this.items.forEach(i => t.push(i.type))
151
152 Object.freeze(t)
153
154 return t
155 }

Callers 1

createDataTransferFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected