Estoy tratando de probar en broma un método de un componente Vue que contiene FileReader. Necesito leer un archivo json.
Método:
readConfig(event) { this.config.headers = []; this.config.keys = []; this.config.rules = []; this.columns = 0 const fr = new FileReader(); fr.onload = e => { const result = JSON.parse(e.target.result); this.config.connectionType = result.connection.type; this.config.url = result.connection.url; if(result.connection.type === 'rest') { this.config.api = result.connection.api; this.config.time = result.connection.time; } else { this.config.socket = result.connection.socket; } result.table.headers.forEach((el, index) => { this.addElement(); this.config.headers[index] = el; this.config.keys[index] = result.table.keys[index]; }) this.config.rules = result.table.rules; } fr.readAsText(event.target.files.item(0)); }Llamado por este archivo de entrada:
<input v-if="!saved" type="file" @change="readConfig" accept=".json">Traté de seguir algunas soluciones, pero no puedo obtener la carga para cargar mi json. Gracias de antemano.
Gracias por la ayuda. Encontré la solución a mi problema.
test('Upload rest configuration', () => { // @ts-ignore jest.spyOn(global, 'FileReader').mockImplementation(function () { // @ts-ignore this.readAsText = jest.fn(); }); wrapper.find('input[type="file"]').trigger('change'); // @ts-ignore let reader = FileReader.mock.instances[0]; reader.onload({ target: { result: JSON.stringify(config) }, files: [{name: 'config.json', size:'20000', type:'text/json'}] }); });