From c310e68659b68cccd4e09b709dd268999e7a1040 Mon Sep 17 00:00:00 2001 From: Nick Litwin Date: Fri, 15 Jan 2016 13:33:04 -0800 Subject: [PATCH 1/2] Add unit tests for tc-form-stockart --- .../tc-form-stockart.directive.js | 3 + .../tc-form-stockart/tc-form-stockart.spec.js | 211 +++++++++++++++++- app/directives/tc-input/tc-input.spec.js | 2 +- app/filters/filters.spec.js | 1 - app/index.jade | 2 - app/specs.html | 7 +- .../submit-file/submit-file.controller.js | 27 ++- 7 files changed, 224 insertions(+), 29 deletions(-) diff --git a/app/directives/tc-form-stockart/tc-form-stockart.directive.js b/app/directives/tc-form-stockart/tc-form-stockart.directive.js index c5c3fca12..c2ae46bdb 100644 --- a/app/directives/tc-form-stockart/tc-form-stockart.directive.js +++ b/app/directives/tc-form-stockart/tc-form-stockart.directive.js @@ -26,6 +26,9 @@ isFileNumberRequired: false }; + // Initialize stockart form data + $scope.formStockarts = { 0: _.assign({id: 0}, angular.copy(emptyStockart)) }; + $scope.urlRegEx = new RegExp(/^(http(s?):\/\/)?(www\.)?[a-zA-Z0-9\.\-\_]+(\.[a-zA-Z]{2,3})+(\/[a-zA-Z0-9\_\-\s\.\/\?\%\#\&\=]*)?$/); $scope.createAdditionalStockartFieldset = function() { diff --git a/app/directives/tc-form-stockart/tc-form-stockart.spec.js b/app/directives/tc-form-stockart/tc-form-stockart.spec.js index fe821738a..09f5f5371 100644 --- a/app/directives/tc-form-stockart/tc-form-stockart.spec.js +++ b/app/directives/tc-form-stockart/tc-form-stockart.spec.js @@ -1,19 +1,216 @@ /* jshint -W117, -W030 */ describe('Topcoder Form Stockart Directive', function() { - var scope; - - // USE AS TEMPLATE FOR DIRECTIVES + var scope, element, isolateScope; beforeEach(function() { - bard.appModule('tcUIComponents'); + bard.appModule('topcoder'); bard.inject(this, '$compile', '$rootScope'); + scope = $rootScope.$new(); + + var form = angular.element('
'); + element = form.find('tc-form-stockart'); + var formElement = $compile(form)(scope); + scope.$digest(); + + isolateScope = element.isolateScope(); }); bard.verifyNoOutstandingHttpRequests(); - xdescribe('', function() { - beforeEach(function() {}); + it('starts with empty stockart data', function() { + defaultFormStockart = isolateScope.formStockarts[0]; + + expect(defaultFormStockart.id).to.equal(0); + expect(defaultFormStockart.description).to.equal(''); + expect(defaultFormStockart.sourceUrl).to.equal(''); + expect(defaultFormStockart.fileNumber).to.equal(''); + expect(defaultFormStockart.isPhotoDescriptionRequired).to.equal(false); + expect(defaultFormStockart.isPhotoURLRequired).to.equal(false); + expect(defaultFormStockart.isFileNumberRequired).to.equal(false); + }); + + describe('createAdditionalStockartFieldset', function() { + it('creates a new fieldset', function() { + expect(Object.keys(isolateScope.formStockarts).length).to.equal(1); + + isolateScope.createAdditionalStockartFieldset(); + scope.$digest(); + + expect(Object.keys(isolateScope.formStockarts).length).to.equal(2); + }); + + it('adds an incremented id to the new fieldset', function() { + var id = isolateScope.formStockarts[0].id; + + isolateScope.createAdditionalStockartFieldset(); + scope.$digest(); + + expect(isolateScope.formStockarts[1]).to.exist; + expect(isolateScope.formStockarts[1].id).to.equal(id + 1); + }); + }) + + describe('deleteStockartFieldset', function() { + it('deletes the selected stockart fieldset', function() { + isolateScope.createAdditionalStockartFieldset(); + scope.$digest(); + + expect(Object.keys(isolateScope.formStockarts).length).to.equal(2); + + isolateScope.deleteStockartFieldset(1); + scope.$digest(); + + expect(Object.keys(isolateScope.formStockarts).length).to.equal(1); + }); + + it('resets the stockart fieldset when it\'s the only one', function() { + var stockart = isolateScope.formStockarts[0]; + + expect(stockart.description).to.equal(''); + + stockart.description = 'a funny cat picture'; + scope.$digest(); + + expect(stockart.description).to.equal('a funny cat picture'); + + isolateScope.deleteStockartFieldset(0); + scope.$digest(); + + expect(isolateScope.formStockarts[0].description).to.equal(''); + }); + }); + + describe('isButtonDisabled', function() { + var button; + + beforeEach(function() { + button = $(element).find('.fieldset__button')[0]; + }); + + afterEach(function() { + button = undefined; + }); + + it('disables the button when no fields are filled out', function() { + expect(button.disabled).to.be.true; + }); + + it('disables the button when 1 field is filled out', function() { + isolateScope.formStockarts[0].description = 'test description'; + scope.$digest(); + + expect(button.disabled).to.be.true; + }); + + it('disables the button when 2 fields are filled out', function() { + isolateScope.formStockarts[0].description = 'test description'; + isolateScope.formStockarts[0].sourceUrl = 'url'; + scope.$digest(); + + expect(button.disabled).to.be.true; + }); + + it('enables the button when all fields are filled out', function() { + isolateScope.formStockarts[0].description = 'test description'; + isolateScope.formStockarts[0].sourceUrl = 'url'; + isolateScope.formStockarts[0].fileNumber = '123'; + scope.$digest(); + + expect(button.disabled).to.be.false; + }); + + it('disables the button when any field in any fieldset is empty', function() { + expect(button.disabled).to.be.true; + + // Fill out first fieldset + isolateScope.formStockarts[0].description = 'test description'; + isolateScope.formStockarts[0].sourceUrl = 'url.com'; + isolateScope.formStockarts[0].fileNumber = '123'; + scope.$digest(); + + expect(button.disabled).to.be.false; + + isolateScope.createAdditionalStockartFieldset(); + scope.$digest(); + + expect(button.disabled).to.be.true; + + // Fill out second fieldset + isolateScope.formStockarts[1].description = 'test description2'; + isolateScope.formStockarts[1].sourceUrl = 'url2.com'; + isolateScope.formStockarts[1].fileNumber = '1232'; + scope.$digest(); + + expect(button.disabled).to.be.false; + + // Empty a field in the first fieldset + isolateScope.formStockarts[0].fileNumber = ''; + scope.$digest(); + + expect(button.disabled).to.be.true; + }); + }) + + describe('showMandatoryMessage', function() { + describe('sets the stockart required properties to false when all fields are', function() { + var stockart; + + beforeEach(function() { + stockart = isolateScope.formStockarts[0]; + stockart.description = 'test description'; + stockart.sourceUrl = 'url.com'; + stockart.fileNumber = '123'; + scope.$digest(); + }); + + afterEach(function() { + stockart = undefined; + }); + + it('filled out', function() { + expect(stockart.isPhotoDescriptionRequired).to.be.false; + expect(stockart.isPhotoURLRequired).to.be.false; + expect(stockart.isFileNumberRequired).to.be.false; + }); + + it('empty', function() { + // Reset stockart fields + stockart.description = ''; + stockart.sourceUrl = ''; + stockart.fileNumber = ''; + scope.$digest(); + + expect(stockart.isPhotoDescriptionRequired).to.be.false; + expect(stockart.isPhotoURLRequired).to.be.false; + expect(stockart.isFileNumberRequired).to.be.false; + }); + }); + + + describe('sets the stockart required properties to false when all fields are', function() { + var stockart; + + beforeEach(function() { + stockart = isolateScope.formStockarts[0]; + stockart.description = 'test description'; + stockart.sourceUrl = 'url.com'; + stockart.fileNumber = '123'; + scope.$digest(); + }); + + afterEach(function() { + stockart = undefined; + }); + + it('sets the stockart required properties to true if any field is blank', function() { + // Reset stockart fields + stockart.description = ''; + scope.$digest(); - it('', function() {}); + expect(stockart.isPhotoDescriptionRequired).to.be.true; + expect(stockart.isPhotoURLRequired).to.be.true; + expect(stockart.isFileNumberRequired).to.be.true; + }); + }); }); }); diff --git a/app/directives/tc-input/tc-input.spec.js b/app/directives/tc-input/tc-input.spec.js index 83a80e6a7..56e369efe 100644 --- a/app/directives/tc-input/tc-input.spec.js +++ b/app/directives/tc-input/tc-input.spec.js @@ -1,6 +1,6 @@ /* jshint -W117, -W030 */ describe('Topcoder Input Directive', function() { - var scope, element, controller; + var scope, element; beforeEach(function() { bard.appModule('topcoder'); diff --git a/app/filters/filters.spec.js b/app/filters/filters.spec.js index fc6a6d662..b3e2c9fc9 100644 --- a/app/filters/filters.spec.js +++ b/app/filters/filters.spec.js @@ -131,7 +131,6 @@ describe('filters', function() { expect(ternaryFilter(true, 1, 2)).to.be.equal(1); expect(ternaryFilter(false, 1, 2)).to.be.equal(2); expect(ternaryFilter(0, 1, 2)).to.be.equal(2); - console.log(jstz.determine().name()); expect(ternaryFilter(true, 'm', 'n')).to.be.equal('m'); }); }); diff --git a/app/index.jade b/app/index.jade index c9ac97625..5354bf8a4 100644 --- a/app/index.jade +++ b/app/index.jade @@ -156,8 +156,6 @@ html script(src='../bower_components/react/react.js') script(src='../bower_components/react/react-dom.js') script(src='../bower_components/classnames/index.js') - script(src='../bower_components/classnames/bind.js') - script(src='../bower_components/classnames/dedupe.js') script(src='../bower_components/react-input-autosize/dist/react-input-autosize.min.js') script(src='../bower_components/react-select/dist/react-select.min.js') script(src='../bower_components/ngReact/ngReact.js') diff --git a/app/specs.html b/app/specs.html index caf9389a9..1c1199ee6 100644 --- a/app/specs.html +++ b/app/specs.html @@ -44,6 +44,7 @@

Spec Runner

+ @@ -69,8 +70,6 @@

Spec Runner

- - @@ -268,8 +267,8 @@

Spec Runner

- + @@ -285,8 +284,8 @@

Spec Runner

- + diff --git a/app/submissions/submit-file/submit-file.controller.js b/app/submissions/submit-file/submit-file.controller.js index 11dfbd604..5c5be8ad9 100644 --- a/app/submissions/submit-file/submit-file.controller.js +++ b/app/submissions/submit-file/submit-file.controller.js @@ -32,17 +32,7 @@ isFontSourceRequired: false } }; - vm.formStockarts = { - 0: { - id: 1, - description: '', - sourceUrl: '', - fileNumber: '', - isPhotoDescriptionRequired: false, - isPhotoURLRequired: false, - isFileNumberRequired: false - } - }; + vm.formStockarts = {}; vm.submissionForm = { files: [], @@ -148,10 +138,18 @@ vm.submissionsBody.data.submitterRank = vm.submissionForm.submitterRank; // Process stock art - var processedStockarts = _.map(vm.formStockarts, function(formStockart) { + var processedStockarts = _.reduce(vm.formStockarts, function(compiledStockarts, formStockart) { + if (formStockart.description) { delete formStockart.id; - return formStockart; - }); + delete formStockart.isPhotoDescriptionRequired; + delete formStockart.isPhotoURLRequired; + delete formStockart.isFileNumberRequired; + + compiledStockarts.push(formStockart); + } + + return compiledStockarts; + }, []); vm.submissionsBody.data.stockArts = processedStockarts; @@ -164,6 +162,7 @@ delete formFont.isFontNameRequired; delete formFont.isFontNameDisabled; delete formFont.isFontSourceRequired; + compiledFonts.push(formFont); } From ec599bd1d34a515a6956fce95e2112d3326a7295 Mon Sep 17 00:00:00 2001 From: Nick Litwin Date: Fri, 15 Jan 2016 15:41:20 -0800 Subject: [PATCH 2/2] Add tests for tc-form-fonts --- .../tc-form-fonts/tc-form-fonts.directive.js | 3 + .../tc-form-fonts/tc-form-fonts.spec.js | 223 +++++++++++++++++- .../tc-form-stockart/tc-form-stockart.spec.js | 28 ++- .../submit-file/submit-file.controller.js | 14 +- 4 files changed, 237 insertions(+), 31 deletions(-) diff --git a/app/directives/tc-form-fonts/tc-form-fonts.directive.js b/app/directives/tc-form-fonts/tc-form-fonts.directive.js index 2b08d47fd..9b097a24e 100644 --- a/app/directives/tc-form-fonts/tc-form-fonts.directive.js +++ b/app/directives/tc-form-fonts/tc-form-fonts.directive.js @@ -42,6 +42,9 @@ isFontSourceRequired: false }; + // Initialize font form data + $scope.formFonts = { 0: _.assign({id: 0}, angular.copy(emptyFont)) }; + $scope.urlRegEx = new RegExp(/^(http(s?):\/\/)?(www\.)?[a-zA-Z0-9\.\-\_]+(\.[a-zA-Z]{2,3})+(\/[a-zA-Z0-9\_\-\s\.\/\?\%\#\&\=]*)?$/); $scope.selectFont = function(newFont) { diff --git a/app/directives/tc-form-fonts/tc-form-fonts.spec.js b/app/directives/tc-form-fonts/tc-form-fonts.spec.js index 5c17c2df7..8f0c5de95 100644 --- a/app/directives/tc-form-fonts/tc-form-fonts.spec.js +++ b/app/directives/tc-form-fonts/tc-form-fonts.spec.js @@ -1,19 +1,228 @@ /* jshint -W117, -W030 */ describe('Topcoder Form Fonts Directive', function() { - var scope; - - // USE AS TEMPLATE FOR DIRECTIVES + var scope, element, isolateScope; beforeEach(function() { - bard.appModule('tcUIComponents'); + bard.appModule('topcoder'); bard.inject(this, '$compile', '$rootScope'); + scope = $rootScope.$new(); + + var form = angular.element('
'); + element = form.find('tc-form-fonts'); + var formElement = $compile(form)(scope); + scope.$digest(); + + isolateScope = element.isolateScope(); }); bard.verifyNoOutstandingHttpRequests(); - xdescribe('', function() { - beforeEach(function() {}); + describe('is initialized with', function() { + it('empty font data', function() { + defaultFormFont = isolateScope.formFonts[0]; + + expect(defaultFormFont.id).to.equal(0); + expect(defaultFormFont.source).to.equal(''); + expect(defaultFormFont.name).to.equal(''); + expect(defaultFormFont.sourceUrl).to.equal(''); + expect(defaultFormFont.isFontUrlRequired).to.be.false; + expect(defaultFormFont.isFontUrlDisabled).to.be.true; + expect(defaultFormFont.isFontNameRequired).to.be.false; + expect(defaultFormFont.isFontNameDisabled).to.be.true; + expect(defaultFormFont.isFontSourceRequired).to.be.false; + }); + + it('a font list', function() { + var fontList = isolateScope.fontList0; + + expect(fontList).to.be.an.array; + expect(fontList).to.have.length.of.at.least(1); + }); + + it('a regular expression', function() { + expect(isolateScope.urlRegEx).to.be.an.instanceof(RegExp); + }); + }); + + describe('selectFont', function() { + var newFont, targetedFont; + + beforeEach(function() { + newFont = { + id: 0, + value: 'FONTS_DOT_COM' + }; + + targetedFont = isolateScope.formFonts[0]; + }); + + afterEach(function() { + newFont = undefined; + targetedFont = undefined; + }); + + it('updates the targeted font source with the new value', function() { + expect(targetedFont.source).to.equal(''); + + isolateScope.selectFont(newFont); + scope.$digest(); + + expect(targetedFont.source).to.equal('FONTS_DOT_COM'); + }); + + it('sets disabled properties to false', function() { + expect(targetedFont.isFontNameDisabled).to.be.true; + expect(targetedFont.isFontUrlDisabled).to.be.true; + + isolateScope.selectFont(newFont); + scope.$digest(); + + expect(targetedFont.isFontNameDisabled).to.be.false; + expect(targetedFont.isFontUrlDisabled).to.be.false; + }); + + it('sets required properties to true', function() { + expect(targetedFont.isFontNameRequired).to.be.false; + expect(targetedFont.isFontUrlRequired).to.be.false; + + isolateScope.selectFont(newFont); + scope.$digest(); + + expect(targetedFont.isFontNameRequired).to.be.true; + expect(targetedFont.isFontUrlRequired).to.be.true; + }); + + it('sets isFontNameRequired to true and isFontUrlRequired to false when STUDIO_STANDARD_FONTS_LIST is selected', function() { + expect(targetedFont.isFontNameRequired).to.be.false; + expect(targetedFont.isFontUrlRequired).to.be.false; + + isolateScope.selectFont({id: 0, value: 'STUDIO_STANDARD_FONTS_LIST'}); + scope.$digest(); + + expect(targetedFont.isFontNameRequired).to.be.true; + expect(targetedFont.isFontUrlRequired).to.be.false; + }); + }); + + describe('createAdditionalFontFieldset', function() { + it('creates a new fieldset', function() { + expect(Object.keys(isolateScope.formFonts).length).to.equal(1); + + isolateScope.createAdditionalFontFieldset(); + scope.$digest(); + + expect(Object.keys(isolateScope.formFonts).length).to.equal(2); + }); + + it('adds an incremented id to the new fieldset', function() { + var id = isolateScope.formFonts[0].id; + + isolateScope.createAdditionalFontFieldset(); + scope.$digest(); + + expect(isolateScope.formFonts[1]).to.exist; + expect(isolateScope.formFonts[1].id).to.equal(id + 1); + }); + }); + + describe('deleteFontFieldset', function() { + it('deletes the selected font fieldset', function() { + isolateScope.createAdditionalFontFieldset(); + scope.$digest(); + + expect(Object.keys(isolateScope.formFonts).length).to.equal(2); + + isolateScope.deleteFontFieldset(1); + scope.$digest(); + + expect(Object.keys(isolateScope.formFonts).length).to.equal(1); + }); + + it('resets the font fieldset when it\'s the only one', function() { + var font = isolateScope.formFonts[0]; + + expect(font.source).to.equal(''); + + font.source = 'dropdown selection'; + scope.$digest(); + + expect(font.source).to.equal('dropdown selection'); + + isolateScope.deleteFontFieldset(0); + scope.$digest(); + + expect(isolateScope.formFonts[0].source).to.equal(''); + }); + }); + + describe('isButtonDisabled', function() { + var button; + + beforeEach(function() { + button = $(element).find('.fieldset__button')[0]; + }); + + afterEach(function() { + button = undefined; + }); + + it('disables the button when no fields are filled out', function() { + expect(button.disabled).to.be.true; + }); + + it('disables the button when 1 field is filled out', function() { + isolateScope.formFonts[0].source = 'FONTS_DOT_COM'; + scope.$digest(); + + expect(button.disabled).to.be.true; + }); + + it('disables the button when 2 fields are filled out', function() { + isolateScope.formFonts[0].source = 'FONTS_DOT_COM'; + isolateScope.formFonts[0].sourceUrl = 'url.com'; + scope.$digest(); + + expect(button.disabled).to.be.true; + }); + + it('enables the button when all fields are filled out', function() { + isolateScope.formFonts[0].source = 'FONTS_DOT_COM'; + isolateScope.formFonts[0].name = 'name'; + isolateScope.formFonts[0].sourceUrl = 'url.com'; + scope.$digest(); + + expect(button.disabled).to.be.false; + }); + + it('disables the button when any field in any fieldset is empty', function() { + expect(button.disabled).to.be.true; + + // Fill out first fieldset + isolateScope.formFonts[0].source = 'FONTS_DOT_COM'; + isolateScope.formFonts[0].name = 'name'; + isolateScope.formFonts[0].sourceUrl = 'url.com'; + scope.$digest(); + + expect(button.disabled).to.be.false; + + isolateScope.createAdditionalFontFieldset(); + scope.$digest(); + + expect(button.disabled).to.be.true; + + // Fill out second fieldset + isolateScope.formFonts[1].source = 'FONTS_DOT_COM2'; + isolateScope.formFonts[1].name = 'name'; + isolateScope.formFonts[1].sourceUrl = 'url2.com'; + scope.$digest(); + + expect(button.disabled).to.be.false; + + // Empty a field in the first fieldset + isolateScope.formFonts[0].name = ''; + scope.$digest(); - it('', function() {}); + expect(button.disabled).to.be.true; + }); }); }); diff --git a/app/directives/tc-form-stockart/tc-form-stockart.spec.js b/app/directives/tc-form-stockart/tc-form-stockart.spec.js index 09f5f5371..ccdf6d1c6 100644 --- a/app/directives/tc-form-stockart/tc-form-stockart.spec.js +++ b/app/directives/tc-form-stockart/tc-form-stockart.spec.js @@ -17,16 +17,22 @@ describe('Topcoder Form Stockart Directive', function() { bard.verifyNoOutstandingHttpRequests(); - it('starts with empty stockart data', function() { - defaultFormStockart = isolateScope.formStockarts[0]; - - expect(defaultFormStockart.id).to.equal(0); - expect(defaultFormStockart.description).to.equal(''); - expect(defaultFormStockart.sourceUrl).to.equal(''); - expect(defaultFormStockart.fileNumber).to.equal(''); - expect(defaultFormStockart.isPhotoDescriptionRequired).to.equal(false); - expect(defaultFormStockart.isPhotoURLRequired).to.equal(false); - expect(defaultFormStockart.isFileNumberRequired).to.equal(false); + describe('is initialized with', function() { + it('empty stockart data', function() { + var initialStockart = isolateScope.formStockarts[0]; + + expect(initialStockart.id).to.equal(0); + expect(initialStockart.description).to.equal(''); + expect(initialStockart.sourceUrl).to.equal(''); + expect(initialStockart.fileNumber).to.equal(''); + expect(initialStockart.isPhotoDescriptionRequired).to.equal(false); + expect(initialStockart.isPhotoURLRequired).to.equal(false); + expect(initialStockart.isFileNumberRequired).to.equal(false); + }); + + it('a regular expression', function() { + expect(isolateScope.urlRegEx).to.be.an.instanceof(RegExp); + }); }); describe('createAdditionalStockartFieldset', function() { @@ -149,7 +155,7 @@ describe('Topcoder Form Stockart Directive', function() { expect(button.disabled).to.be.true; }); - }) + }); describe('showMandatoryMessage', function() { describe('sets the stockart required properties to false when all fields are', function() { diff --git a/app/submissions/submit-file/submit-file.controller.js b/app/submissions/submit-file/submit-file.controller.js index 5c5be8ad9..cfb8606bd 100644 --- a/app/submissions/submit-file/submit-file.controller.js +++ b/app/submissions/submit-file/submit-file.controller.js @@ -19,19 +19,7 @@ vm.finishing = false; vm.showProgress = false; vm.errorInUpload = false; - vm.formFonts = { - 0: { - id: 0, - source: '', - name: '', - sourceUrl: '', - isFontUrlRequired: false, - isFontUrlDisabled: true, - isFontNameRequired: false, - isFontNameDisabled: true, - isFontSourceRequired: false - } - }; + vm.formFonts = {}; vm.formStockarts = {}; vm.submissionForm = { files: [],