Created
June 18, 2012 19:14
-
-
Save EndangeredMassa/2950153 to your computer and use it in GitHub Desktop.
JavaScript Testing Best Practices: Part 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| view = Backbone.View.extend | |
| initialize: | |
| @render() | |
| render: | |
| $el.html('<div class="content">fun times</div>') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| src_files: | |
| # your normal script files | |
| helpers: | |
| - helpers/suiteHealthHelper.js | |
| spec_files: | |
| - "**/*[sS]pec.js" | |
| - spec/javascripts/support/suite_health.js | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # calculate total | |
| subtotal = getSubTotal() | |
| state = getState() | |
| tax = getTax(subtotal, state) | |
| discounts = getDiscounts() | |
| total = subtotal + tax - discounts | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| calculateTotal = -> | |
| subtotal = getSubTotal() | |
| state = getState() | |
| tax = getTax(subtotal, state) | |
| discounts = getDiscounts() | |
| return subtotal + tax - discounts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| describe 'Jasmine Suite', -> | |
| it 'has no errors from window.onerror', -> | |
| expect(JSON.stringify(window.__errors)).toBe('[]') | |
| describe 'does not pollute global state', -> | |
| it 'by setting cookies', -> | |
| expect(document.cookie).toBe('') | |
| it 'by setting local storage values', -> | |
| actual = '' | |
| for key in localStorage | |
| value = localStorage.getItem(key) | |
| actual += "#{key}:#{value};" | |
| expect(actual).toBe('') | |
| it 'by setting session storage values', -> | |
| actual = '' | |
| for key in sessionStorage | |
| value = sessionStorage.getItem(key) | |
| actual += "#{key}:#{value};" | |
| expect(actual).toBe('') | |
| it 'by leaving DOM elements in the document', -> | |
| $pollution = $('body>*:not(#jasmine_content, .jasmine_reporter)') | |
| expected = "Pollution Elements Count: #{$pollution.length}" | |
| expect(expected).toBe("Pollution Elements Count: 0") | |
| describe 'does not log to the console', -> | |
| it 'with console.log', -> | |
| expect(JSON.stringify(window.consoleLogs)).toBe('[]') | |
| it 'with console.dir', -> | |
| expect(JSON.stringify(window.consoleDirs)).toBe('[]') | |
| it 'with console.warn', -> | |
| expect(JSON.stringify(window.consoleWarns)).toBe('[]') | |
| it 'with console.error', -> | |
| expect(JSON.stringify(window.consoleErrors)).toBe('[]') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| window.__errors = [] | |
| window.onerror = (message, file, line) -> | |
| window.__errors.push | |
| message: message | |
| file: file | |
| line: line | |
| return false | |
| if (window.addEventListener) | |
| window.addEventListener 'onerror', (message, file, line) -> | |
| window.__errors.push | |
| message: message | |
| file: file | |
| line: line | |
| return false | |
| window.consoleLogs = [] | |
| window.consoleDirs = [] | |
| window.consoleWarns = [] | |
| window.consoleErrors = [] | |
| if (window['console']) | |
| _consoleLog = console.log | |
| window.console.log = (message) -> | |
| _consoleLog(message) | |
| window.consoleLogs.push(message) | |
| _consoleDir = console.dir | |
| window.console.dir = (message) -> | |
| _consoleDir(message) | |
| window.consoleDirs.push(message) | |
| _consoleWarn = console.warn | |
| window.console.warn = (message) -> | |
| _consoleWarn(message) | |
| window.consoleWarns.push(message) | |
| _consoleError = console.error | |
| window.console.error = (message) -> | |
| _consoleError(message) | |
| window.consoleErrors.push(message) | |
| window.alert = (message) -> | |
| errorMessage = 'Cannot call alert in specs. Called with: ' + message | |
| throw new Error(errorMessage) | |
| window.confirm = (message) -> | |
| errorMessage = 'Cannot call confirm in specs. Called with: ' + message | |
| throw new Error(errorMessage) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #TODO: Move this to another file | |
| processOrders = -> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment