declare var testHarness;

(function() {

	'use strict';

	describe('Console Directive', function() {

		var $compile;
		var $rootScope;
		var ufConsole;
		var scope;
		var vm;
		var element;



		testHarness.beforeEachCreateMocks();
	
		beforeEach(angular.mock.module('public/console/console.html'));

		beforeEach(
			inject(function(_$compile_, _$rootScope_, _ufConsole_) {
				$compile = _$compile_;
				$rootScope = _$rootScope_;
				ufConsole = _ufConsole_;
			})
		);


		beforeEach(function() {

			element = angular.element('<uf-console></uf-console>');
			element = $compile(element)($rootScope.$new());

			$rootScope.$digest();

			vm = element.controller('ufConsole');
			scope = element.isolateScope();

		});

		afterEach(function() {
			scope.$destroy();
		});


		it('should compile and display messages', function() {

			vm.messages = [{
				type: ufConsole.msgTypes.INSTALL,
				text: 'Test message 1'
			}, {
				type: ufConsole.msgTypes.STATUS,
				text: 'Test message 2'
			}];

			scope.$digest();

			var messageSpans = element.find('span');
			expect(messageSpans.length).toBe(2);
			expect(angular.element(messageSpans[0]).html()).toContain('Test message 1');
			expect(angular.element(messageSpans[1]).html()).toContain('Test message 2');

		});

		it('should update the displayed messages', function() {
		
			vm.messages = [{
				type: ufConsole.msgTypes.INSTALL,
				text: 'Test message 1'
			}, {
				type: ufConsole.msgTypes.STATUS,
				text: 'Test message 2'
			}];

			scope.$digest();

			var messageSpans = element.find('span');
			expect(messageSpans.length).toBe(2);

			vm.messages.push({
				type : ufConsole.msgTypes.INSTALL,
				text : 'Test message 3'
			});

			scope.$digest();

			messageSpans = element.find('span');
			expect(messageSpans.length).toBe(3);

		});

		it('should update console view when messages are added to the console service', function(){
			var testMessageBody = 'console service add messsage test';

			ufConsole.addMessage(ufConsole.msgTypes.STATUS, testMessageBody);

			var messageSpans = element.find('span');
			expect(messageSpans.length).toBe(1);
			expect(angular.element(messageSpans[0]).html()).toContain(testMessageBody);

			// add a few more messages
			ufConsole.addMessage(ufConsole.msgTypes.STATUS, testMessageBody + '1');
			ufConsole.addMessage(ufConsole.msgTypes.STATUS, testMessageBody + '2');

			messageSpans = element.find('span');
			expect(messageSpans.length).toBe(3);
			expect(angular.element(messageSpans[0]).html()).toContain(testMessageBody);
			expect(angular.element(messageSpans[1]).html()).toContain(testMessageBody + '1');
			expect(angular.element(messageSpans[2]).html()).toContain(testMessageBody + '2');


		});

	});

})();