Created
June 22, 2016 13:28
-
-
Save mrosata/746f98b3e41d12384d7c7c49f04a585d to your computer and use it in GitHub Desktop.
Integration | Component | signup form
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
| import Em from 'ember'; | |
| const birthdayRE = /\d{4}-\d{2}-\d{2}/; | |
| export default Em.Component.extend({ | |
| classNames: ['col-md-12', 'section-post-comment'], | |
| title: 'Sign-up', | |
| /** | |
| * Because components are singletons, we should re-create the field values any | |
| * time that the form is re-inserted on the page | |
| */ | |
| willInsertElement() { | |
| this._super(...arguments); | |
| Em.run.once(() => { | |
| this.set('formValues', Em.Object.create({ | |
| email: '', | |
| username: '', | |
| firstName: '', | |
| lastName: '', | |
| birthday: '', | |
| gender: 'unspecified', | |
| // These are just the field values. | |
| createPassword: '', | |
| confirmPassword: '' | |
| })); | |
| }); | |
| }, | |
| /** | |
| * Computed property which reflects whether the form has been completely filled out. | |
| * //TODO: Better validation would be nice. IE: Email | |
| */ | |
| submitIsDisabled: Em.computed('formValues.createPassword', 'formValues.confirmPassword', 'formValues.username', | |
| 'formValues.firstName', 'formValues.lastName', 'formValues.email', | |
| function() { | |
| if (!this.get('formValues.username') || this.get('formValues.username.length') < 4) { | |
| return true; | |
| } | |
| if (this.get('formValues.confirmPassword') !== this.get('formValues.createPassword') || this.get('formValues.confirmPassword') < 4 ) { | |
| return true; | |
| } | |
| }), | |
| /** | |
| * Send submitted form data out to handler. | |
| * Triggered from action fired on submit. | |
| * | |
| * @returns {void} | |
| * @private | |
| */ | |
| _sendSubmitForm() { | |
| // Send the formValues out to be handled by external container. | |
| this.sendAction('handleSubmitForm', this.get('formValues')); | |
| }, | |
| actions: { | |
| onSubmitForm() { | |
| this._sendSubmitForm(...arguments); | |
| } | |
| } | |
| }); | |
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
| import Ember from 'ember'; | |
| export default Ember.Controller.extend({ | |
| appName: 'App Lee Name Dah' | |
| }); |
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
| import Ember from 'ember'; | |
| export default Ember.Route.extend({ | |
| actions: { | |
| onSubmitForm(newUser) { | |
| alert(`Signup for ${newUser.username}`); | |
| } | |
| } | |
| }); |
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
| import Resolver from '../../resolver'; | |
| import config from '../../config/environment'; | |
| const resolver = Resolver.create(); | |
| resolver.namespace = { | |
| modulePrefix: config.modulePrefix, | |
| podModulePrefix: config.podModulePrefix | |
| }; | |
| export default resolver; |
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
| import { moduleForComponent, test } from 'ember-qunit'; | |
| import hbs from 'htmlbars-inline-precompile'; | |
| import Em from 'ember'; | |
| moduleForComponent('signup-form', 'Integration | Component | signup form', { | |
| integration: true | |
| }); | |
| test('it renders the title properly', function(assert) { | |
| this.render(hbs`{{signup-form}}`); | |
| assert.notEqual(this.$().text().trim(), ''); | |
| // Template block usage: | |
| this.render(hbs` | |
| {{#signup-form title='Test title wakka wakka'}} | |
| ------------------------><--------------------- | |
| {{/signup-form}} | |
| `); | |
| assert.equal(this.$('.headline-row .title').text().trim(), 'Test title wakka wakka'); | |
| }); | |
| /** | |
| * Test that when values are placed into the form that submission | |
| * then becomes possible and upon a submission the values from | |
| * the form are sent via the "onSubmitForm" action. | |
| */ | |
| test('it triggers the onSubmitForm action upon submission', function(assert) { | |
| assert.expect(3); | |
| let expected = { | |
| email: 'lah-dah@tah-dun.dah-dha', | |
| username: 'zippyZappShaDoop', | |
| firstName: 'Zip', | |
| lastName: 'PeeZapShaDoop', | |
| password: '1Two3Floor', | |
| }; | |
| this.set('actions', Em.Object.create()); | |
| this.set('onSubmitForm', (actual = {}) => { | |
| let actualParsed = { | |
| email: actual.get('email'), | |
| username: actual.get('username'), | |
| firstName: actual.get('firstName'), | |
| lastName: actual.get('lastName'), | |
| password: actual.get('createPassword'), | |
| }; | |
| assert.deepEqual(actualParsed, expected, 'Passes correct data to onFormSubmit handlers'); | |
| }); | |
| /* 'formValues' on this 'test' scope will be passed into | |
| * the inline hbs which renders the {{signup-form}}. Under | |
| * normal situations this data would be filled in through | |
| * inputs by the user, but for testing purposes it will be | |
| * manipulated using this.set('formValues.<something>') so | |
| * we can see that the values are acurately passed out in | |
| * the action 'handleSubmitForm' triggered in the component | |
| * when the forms submit is triggered. | |
| */ | |
| this.set('formValues', Em.Object.create()); | |
| this.render(hbs`{{signup-form handleSubmitForm=onSubmitForm formValues=formValues}}`); | |
| assert.equal(this.$('button[type=submit]').attr('disabled'), 'disabled', 'Form should not be ready before filled out'); | |
| // Fill out the form | |
| this.set('formValues.email', expected.email); | |
| this.set('formValues.username', expected.username); | |
| this.set('formValues.firstName', expected.firstName); | |
| this.set('formValues.lastName', expected.lastName); | |
| this.set('formValues.createPassword', expected.password); | |
| this.set('formValues.confirmPassword', expected.password); | |
| assert.equal(this.$('button[type=submit]').attr('disabled'), undefined, 'Form should be ready with all fields filled out correctly'); | |
| this.$('button[type=submit]').click(); | |
| }); | |
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
| import resolver from './helpers/resolver'; | |
| import { | |
| setResolver | |
| } from 'ember-qunit'; | |
| setResolver(resolver); |
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
| { | |
| "version": "0.9.3", | |
| "EmberENV": { | |
| "FEATURES": {} | |
| }, | |
| "options": { | |
| "use_pods": false, | |
| "enable-testing": true | |
| }, | |
| "dependencies": { | |
| "jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
| "ember": "2.6.0", | |
| "ember-data": "2.6.1", | |
| "ember-template-compiler": "2.6.0" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment