From 804507ef4925cafdf5349a3b7fa1e41dbca87ff7 Mon Sep 17 00:00:00 2001 From: Julien Mougenot Date: Wed, 11 Mar 2020 09:38:30 +0000 Subject: [REF] *: Adapt all module tests to the new control panel Use of the new control panel helpers to increase consistency and change the assertions according to the new DOM/behaviour (e.g. components removed instead of turning invisible). Part of task 2196029 Co-authored-by: Mathieu Duckerts-Antoine --- .../tests/components/custom_file_input_tests.js | 141 ++++++++++----------- 1 file changed, 64 insertions(+), 77 deletions(-) (limited to 'addons/web/static/tests/components/custom_file_input_tests.js') diff --git a/addons/web/static/tests/components/custom_file_input_tests.js b/addons/web/static/tests/components/custom_file_input_tests.js index 0131213f6e9..279bf67f11b 100644 --- a/addons/web/static/tests/components/custom_file_input_tests.js +++ b/addons/web/static/tests/components/custom_file_input_tests.js @@ -2,102 +2,89 @@ odoo.define('web.custom_file_input_tests', function (require) { "use strict"; const CustomFileInput = require('web.CustomFileInput'); - const makeTestEnvironment = require('web.test_env'); const testUtils = require('web.test_utils'); - const { Component, tags, useState } = owl; - const { xml } = tags; + const { createComponent } = testUtils; QUnit.module('Components', {}, function () { // This module cannot be tested as thoroughly as we want it to be: // browsers do not let scripts programmatically assign values to inputs // of type file - QUnit.module('CustomFileInput', {}, function () { - QUnit.test("Rendering of all props", async function (assert) { - assert.expect(12); - - class Parent extends Component { - constructor() { - super(...arguments); - this.state = useState({ innerContent: 'Trigger' }); - } - // Handlers - _onFileUploaded(ev) { - assert.strictEqual(ev.detail.files.length, 0, - "'files' property should be an empty array"); - } - } - Parent.components = { CustomFileInput }; - Parent.env = makeTestEnvironment({ + QUnit.module('CustomFileInput'); + + QUnit.test("Upload a file: default props", async function (assert) { + assert.expect(6); + + const customFileInput = await createComponent(CustomFileInput, { + env: { + services: { + async httpRequest(route, params) { + assert.deepEqual(params, { + csrf_token: odoo.csrf_token, + ufile: [], + }); + assert.step(route); + return '[]'; + }, + }, + }, + }); + const input = customFileInput.el.querySelector('input'); + + assert.strictEqual(customFileInput.el.innerText.trim().toUpperCase(), "CHOOSE FILE", + "File input total text should match its given inner element's text"); + assert.strictEqual(input.accept, '*', + "Input should accept all files by default"); + + await testUtils.dom.triggerEvent(input, 'change'); + + assert.notOk(input.multiple, "'multiple' attribute should not be set"); + assert.verifySteps(['/web/binary/upload']); + + customFileInput.destroy(); + }); + + QUnit.test("Upload a file: custom attachment", async function (assert) { + assert.expect(6); + + const customFileInput = await createComponent(CustomFileInput, { + env: { services: { - httpRequest: (route, params) => { - if (route === '/web/binary/upload') { - assert.deepEqual(params, { - csrf_token: odoo.csrf_token, - ufile: [], - }); - assert.step(route); - return Promise.resolve('[]'); - } else if (route === '/web/binary/upload_attachment') { - assert.deepEqual(params, { - id: 5, - model: 'res.model', - csrf_token: odoo.csrf_token, - ufile: [], - }); - assert.step(route); - return Promise.resolve('[]'); - } + async httpRequest(route, params) { + assert.deepEqual(params, { + id: 5, + model: 'res.model', + csrf_token: odoo.csrf_token, + ufile: [], + }); + assert.step(route); + return '[]'; }, }, - }); - Parent.template = xml` - - - `; - - const parent = new Parent(); - await parent.mount(testUtils.prepareTarget()); - const fileInput = parent.el; - const input = fileInput.querySelector('input'); - - // Default props - assert.strictEqual(fileInput.innerText.trim(), "Trigger", - "File input total text should match its given inner element's text"); - assert.strictEqual(input.accept, '*', - "Input should accept all files by default"); - await testUtils.dom.triggerEvent(input, 'change'); - assert.notOk(input.multiple, - "'multiple' attribute should not be set"); - - // Change props - Object.assign(parent.state, { + }, + props: { accepted_file_extensions: '.png', action: '/web/binary/upload_attachment', id: 5, model: 'res.model', multi_upload: true, - }); - await testUtils.nextTick(); + }, + intercepts: { + 'uploaded': ev => assert.strictEqual(ev.detail.files.length, 0, + "'files' property should be an empty array"), + }, + }); + const input = customFileInput.el.querySelector('input'); - assert.strictEqual(input.accept, '.png', - "Input should now only accept pngs"); - await testUtils.dom.triggerEvent(input, 'change'); - assert.ok(input.multiple, - "'multiple' attribute should be set"); + assert.strictEqual(input.accept, '.png', "Input should now only accept pngs"); - assert.verifySteps(['/web/binary/upload', '/web/binary/upload_attachment']); + await testUtils.dom.triggerEvent(input, 'change'); - parent.destroy(); - }); + assert.ok(input.multiple, "'multiple' attribute should be set"); + assert.verifySteps(['/web/binary/upload_attachment']); + + customFileInput.destroy(); }); }); }); -- cgit v1.2.3