<commit_before>'use strict';

describe('the view recipe controller', function () {

	beforeEach(module('recipe'));

	describe('the scope canAddToRecipeBook function', function () {
		var scope;
		var compile;
		var recipeBookService;
		var recipeBook = [{recipeId: 12}, {recipeId: 13}];
		var recipeBookPromise;

		function buildControllerForRecipeId(options) {
			inject(function ($controller, $rootScope, $compile, $q) {
				var recipeIdToUse = options.withRecipeInRecipeBook ? 12 : 42;
				scope = $rootScope.$new();
				compile = $compile;
				recipeBookService = {};
				var recipeBookDeferred = $q.defer();
				recipeBookPromise = recipeBookDeferred.promise;
				recipeBookDeferred.resolve(recipeBook);
				recipeBookService.getRecipeBook = jasmine.createSpy('getRecipeBook').and.returnValue(recipeBookPromise);
				$controller('ViewRecipeCtrl', {
					$scope: scope,
					$routeParams: {recipeId: recipeIdToUse},
					recipeBookService: recipeBookService
				});
			});
		}

		function initializeController(options) {
			angular.mock.inject(function (userService, $httpBackend) {
				var recipeIdToUse = options.withRecipeInRecipeBook ? 12 : 42;
				$httpBackend.expect('GET', 'api/recipe/' + recipeIdToUse).respond({recipeId: recipeIdToUse});

				$httpBackend.flush();
			});
		}

		it('will return false if no user is logged in', function (done) {
			angular.mock.inject(function (userService) {
				var options = {withRecipeInRecipeBook: false};
				spyOn(userService, 'isLoggedIn').and.returnValue(false);

				buildControllerForRecipeId(options)
				initializeController(options);

				recipeBookPromise.then(function () {
					var result = scope.canAddToRecipeBook();
					expect(result).toBe(false);
				}).then(done, done.fail);

				scope.$digest();
			});
		});

		it('will return false if the current recipe is in the logged in users recipe book', function (done) {
			angular.mock.inject(function (userService) {
				var options = {withRecipeInRecipeBook: true};
				spyOn(userService, 'isLoggedIn').and.returnValue(true);

				buildControllerForRecipeId(options)
				initializeController(options);

				recipeBookPromise.then(function () {
					var result = scope.canAddToRecipeBook();
					expect(result).toBe(false);
				}).then(done, done.fail);

				scope.$digest();
			});
		});

		it('will return false if in editMode', function (done) {
			angular.mock.inject(function (userService) {
				var options = {withRecipeInRecipeBook: false};
				spyOn(userService, 'isLoggedIn').and.returnValue(true);

				buildControllerForRecipeId(options)
				initializeController(options);

				scope.editClicked();

				recipeBookPromise.then(function () {
					var result = scope.canAddToRecipeBook();
					expect(result).toBe(false);
				}).then(done, done.fail);

				scope.$digest();
			});
		});

		it('will return true if the user is logged in and the page is not in edit mode and the recipe is in the users recipe book', function (done) {
			angular.mock.inject(function (userService) {
				var options = {withRecipeInRecipeBook: false};
				spyOn(userService, 'isLoggedIn').and.returnValue(true);

				buildControllerForRecipeId(options)
				initializeController(options);

				recipeBookPromise.then(function () {
					var result = scope.canAddToRecipeBook();
					expect(result).toBe(true);
				}).then(done, done.fail);

				scope.$digest();
			});
		});
	});

	describe('when given the html used in the application for the /view-recipe/:recipeId route', function () {

		var scope;
		var compile;

		beforeEach(inject(function ($controller, $rootScope, $compile, $q) {
			scope = $rootScope.$new();
			compile = $compile;
			$controller('ViewRecipeCtrl', {
				$scope: scope
			});
		}));

		function loadPage($httpBackend) {
			$httpBackend.expect('GET', 'api/recipe/undefined').respond({});
			$httpBackend.expect('GET', '/api/user/undefined/recipe-book').respond({});
			$httpBackend.expect('GET', 'recipe-lib/user/user.html').respond('<div></div>');

			jasmine.getFixtures().fixturesPath = 'base/app/recipe-lib/view-recipe';
			loadFixtures('view-recipe.html');<commit_msg>refactoring some recent unit tests<commit_after>'use strict';

function buildMockPromiseFunction(valueToResolveTo) {
	var valuePromise = undefined;
	inject(function ($q) {
		var valueDeferred = $q.defer();
		valuePromise = valueDeferred.promise;
		valueDeferred.resolve(valueToResolveTo);
	});

	return jasmine.createSpy('').and.returnValue(valuePromise);;
}

describe('the view recipe controller', function () {

	beforeEach(module('recipe'));

	describe('the scope canAddToRecipeBook function', function () {
		var scope;
		var compile;
		var recipeBookService;
		var recipeBook = [{recipeId: 12}, {recipeId: 13}];

		function buildControllerForRecipeId(options) {
			inject(function ($controller, $rootScope, $compile, $q) {
				var recipeIdToUse = options.withRecipeInRecipeBook ? 12 : 42;
				scope = $rootScope.$new();
				compile = $compile;

				recipeBookService = {};
				recipeBookService.getRecipeBook = buildMockPromiseFunction(recipeBook);

				$controller('ViewRecipeCtrl', {
					$scope: scope,
					$routeParams: {recipeId: recipeIdToUse},
					recipeBookService: recipeBookService
				});
			});
		}

		function initializeController(options) {
			angular.mock.inject(function (userService, $httpBackend) {
				var recipeIdToUse = options.withRecipeInRecipeBook ? 12 : 42;
				$httpBackend.expect('GET', 'api/recipe/' + recipeIdToUse).respond({recipeId: recipeIdToUse});

				$httpBackend.flush();
			});
		}

		it('will return false if no user is logged in', function () {
			angular.mock.inject(function (userService) {
				var options = {withRecipeInRecipeBook: false};
				spyOn(userService, 'isLoggedIn').and.returnValue(false);

				buildControllerForRecipeId(options)
				initializeController(options);

				var result = scope.canAddToRecipeBook();
				expect(result).toBe(false);
			});
		});

		it('will return false if the current recipe is in the logged in users recipe book', function () {
			angular.mock.inject(function (userService) {
				var options = {withRecipeInRecipeBook: true};
				spyOn(userService, 'isLoggedIn').and.returnValue(true);

				buildControllerForRecipeId(options)
				initializeController(options);

				var result = scope.canAddToRecipeBook();
				expect(result).toBe(false);
			});
		});

		it('will return false if in editMode', function () {
			angular.mock.inject(function (userService) {
				var options = {withRecipeInRecipeBook: false};
				spyOn(userService, 'isLoggedIn').and.returnValue(true);

				buildControllerForRecipeId(options)
				initializeController(options);

				scope.editClicked();

				var result = scope.canAddToRecipeBook();
				expect(result).toBe(false);
			});
		});

		it('will return true if the user is logged in and the page is not in edit mode and the recipe is in the users recipe book', function () {
			angular.mock.inject(function (userService) {
				var options = {withRecipeInRecipeBook: false};
				spyOn(userService, 'isLoggedIn').and.returnValue(true);

				buildControllerForRecipeId(options)
				initializeController(options);

				var result = scope.canAddToRecipeBook();
				expect(result).toBe(true);
			});
		});
	});

	describe('when given the html used in the application for the /view-recipe/:recipeId route', function () {

		var scope;
		var compile;

		beforeEach(inject(function ($controller, $rootScope, $compile) {
			scope = $rootScope.$new();
			compile = $compile;
			$controller('ViewRecipeCtrl', {
				$scope: scope
			});
		}));

		function loadPage($httpBackend) {
			$httpBackend.expect('GET', 'api/recipe/undefined').respond({});
			$httpBackend.expect('GET', '/api/user/undefined/recipe-book').respond({});
			$httpBackend.expect('GET', 'recipe-lib/user/user.html').respond('<div></div>');

			jasmine.getFixtures().fixturesPath = 'base/app/recipe-lib/view-recipe';
			loadFixtures('view-recipe.html');
{'fraction_non_alphanumeric': 0.13939699760312854, 'fraction_numerical': 0.0030276270972625207, 'mean_word_length': 4.385869565217392, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6605140', 'n_tokens_mistral': 2925, 'n_tokens_neox': 2428, 'n_words': 493}