<commit_before>/* global spyOn, expect */

/**
 * @author <NAME> <<EMAIL>>
 *
 * Mail
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

define([
	'radio',
	'views/setupview'
], function(Radio, SetupView) {

	describe('Setup view', function() {
		var view,
			loadingPromise,
			resolvePromise,
			rejectPromise;

		/**
		 * @param {Object} config
		 * @returns {Promise}
		 */
		function triggerFormSubmit(config) {
			return view.onChildviewFormSubmit(config);
		}

		beforeEach(function() {
			view = new SetupView();
			loadingPromise = new Promise(function(resolve, reject) {
				resolvePromise = resolve;
				rejectPromise = reject;
			});
		});

		it('shows the setup form', function() {
			view.render();
			expect(view.$el.html()).toContain('Connect your mail account');
		});

		it('shows a loading view while the account is being created', function() {
			spyOn(Radio.account, 'request').and.returnValue(loadingPromise);
			spyOn(Radio.ui, 'trigger');

			// Submit -> show loading view
			triggerFormSubmit({});
			expect(Radio.account.request).toHaveBeenCalledWith('create', {});
			expect(view.$el.html()).toContain('Setting up your account');
		});

		it('show the form again after an error occurred', function(done) {
			spyOn(Radio.account, 'request').and.returnValue(loadingPromise);
			spyOn(Radio.ui, 'trigger');
			var config = {
				accountName: 'User',
				emailAddress: '<EMAIL>',
				password: '<PASSWORD>',
				imapHost: 'imap.example.com',
				imapUser: '<EMAIL>',
				imapPassword: '<PASSWORD>',
				smtpHost: 'smtp.example.com',
				smtpUser: '<EMAIL>',
				smtpPassword: '<PASSWORD>',
				autoDetect: false
			};

			// Reject loading promise -> show error view
			rejectPromise('Some error');
			triggerFormSubmit(config).then(function() {
				expect(Radio.account.request).toHaveBeenCalledWith('create', config);
				expect(view.$el.html()).toContain('Connect your mail account');
				expect(view.$el.html()).toContain('User');
				expect(view.$el.html()).toContain('<EMAIL>');
				expect(view.$el.html()).toContain('123456');
				
				expect(view.$el.html()).toContain('imap.example.com');
				expect(view.$el.html()).toContain('<EMAIL>');
				expect(view.$el.html()).toContain('i1234');<commit_msg>Fix js tests to reflect changes in error view<commit_after>/* global spyOn, expect */

/**
 * @author <NAME> <<EMAIL>>
 *
 * Mail
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

define([
	'radio',
	'views/setupview'
], function(Radio, SetupView) {

	describe('Setup view', function() {
		var view,
			loadingPromise,
			resolvePromise,
			rejectPromise;

		/**
		 * @param {Object} config
		 * @returns {Promise}
		 */
		function triggerFormSubmit(config) {
			return view.onChildviewFormSubmit(config);
		}

		beforeEach(function() {
			view = new SetupView();
			loadingPromise = new Promise(function(resolve, reject) {
				resolvePromise = resolve;
				rejectPromise = reject;
			});
		});

		it('shows the setup form', function() {
			view.render();
			expect(view.$el.html()).toContain('Connect your mail account');
		});

		it('shows a loading view while the account is being created', function() {
			spyOn(Radio.account, 'request').and.returnValue(loadingPromise);
			spyOn(Radio.ui, 'trigger');

			// Submit -> show loading view
			triggerFormSubmit({});
			expect(Radio.account.request).toHaveBeenCalledWith('create', {});
			expect(view.$el.html()).toContain('Setting up your account');
		});

		it('shows the form again after an error occurred', function(done) {
			spyOn(Radio.account, 'request').and.returnValue(loadingPromise);
			spyOn(Radio.ui, 'trigger');
			var config = {
				accountName: 'User',
				emailAddress: '<EMAIL>',
				password: '<PASSWORD>',
				imapHost: 'imap.example.com',
				imapUser: '<EMAIL>',
				imapPassword: '<PASSWORD>',
				smtpHost: 'smtp.example.com',
				smtpUser: '<EMAIL>',
				smtpPassword: '<PASSWORD>',
				autoDetect: false
			};

			// Reject loading promise -> show error view
			rejectPromise('Some error');
			triggerFormSubmit(config).then(function() {
				expect(view.$el.html()).toContain('Try again');

				// Click 'Try again'
				view.$el.find('button').click();

				expect(Radio.account.request).toHaveBeenCalledWith('create', config);
				expect(view.$el.html()).toContain('Connect your mail account');
				expect(view.$el.html()).toContain('User');
				expect(view.$el.html()).toContain('<EMAIL>');
				expect(view.$el.html()).toContain('123456');

				expect(view.$el.html()).toContain('imap.example.com');
				expect(view.$el.html()).toContain('<EMAIL>');
				expect(view.$el.html()).toContain('i1234');
{'fraction_non_alphanumeric': 0.15232133541992696, 'fraction_numerical': 0.004173187271778821, 'mean_word_length': 3.9671848013816926, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6177952', 'n_tokens_mistral': 2006, 'n_tokens_neox': 1717, 'n_words': 465}