Skip to content

Commit

Permalink
feat: subscription test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
tejaskh3 committed Oct 23, 2024
1 parent cf4fc6f commit 2417931
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/constants/regex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const phoneNumberRegex =
/^[+]{1}(?:[0-9\-\\(\\)\\/.]\s?){6,15}[0-9]{1}$/;
116 changes: 116 additions & 0 deletions tests/integration/components/subscribe-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render, fillIn, click } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';

module('Integration | Component | subscribe', function (hooks) {
setupRenderingTest(hooks);

test('subscribe form renders and functions correctly', async function (assert) {
assert.expect(8);

this.set('email', '[email protected]');
this.set('phone', '1234567890');
this.set('isSubscribed', false);

this.set('updateEmail', (event) => {
this.set('email', event.target.value);
});

this.set('updatePhone', (event) => {
this.set('phone', event.target.value);
});

this.set('onSubmit', (event) => {
event.preventDefault();
this.set('isSubscribed', true);
});

await render(hbs`
<form {{on "submit" this.onSubmit}} data-test-subscribe-form>
<label for="email" data-test-email-label>Email:</label>
<input
type="email"
id="email"
value={{this.email}}
data-test-email-input
{{on "input" this.updateEmail}}
/>
<label for="phone" data-test-phone-label>Phone:</label>
<input
type="tel"
id="phone"
value={{this.phone}}
data-test-phone-input
{{on "input" this.updatePhone}}
/>
<button type="submit" data-test-submit-button>Subscribe</button>
</form>
`);

assert
.dom('[data-test-email-label]')
.hasText('Email:', 'Email label is rendered');

assert
.dom('[data-test-phone-label]')
.hasText('Phone:', 'Phone label is rendered');

assert
.dom('[data-test-submit-button]')
.hasText('Subscribe', 'Submit button is rendered');

assert
.dom('[data-test-email-input]')
.hasValue('[email protected]', 'Email initial value is correct');

assert
.dom('[data-test-phone-input]')
.hasValue('1234567890', 'Phone initial value is correct');

await fillIn('[data-test-email-input]', '[email protected]');
await fillIn('[data-test-phone-input]', '0987654321');

assert
.dom('[data-test-email-input]')
.hasValue('[email protected]', 'Email value updates correctly');

assert
.dom('[data-test-phone-input]')
.hasValue('0987654321', 'Phone value updates correctly');

await click('[data-test-submit-button]');

assert.ok(
this.isSubscribed,
'User should be subscribed after form submission',
);
});

test('subscribe form prevents submission with invalid email', async function (assert) {
this.set('email', '');
this.set('phone', '1234567890');
this.set('isSubscribed', false);

await render(hbs`
<form {{on "submit" this.onSubmit}} data-test-subscribe-form>
<label for="email" data-test-email-label>Email:</label>
<input type="email" value={{this.email}} data-test-email-input />
<input type="tel" value={{this.phone}} data-test-phone-input />
<label for="phone" data-test-email-label>Phone:</label>
<button type="submit" data-test-submit-button>Subscribe</button>
</form>
`);

assert
.dom('[data-test-submit-button]')
.isDisabled('Submit button is disabled with an empty email');

await fillIn('[data-test-email-input]', 'invalidemail');
assert
.dom('[data-test-submit-button]')
.isDisabled('Submit button is disabled with an invalid email');
});
});
46 changes: 46 additions & 0 deletions tests/unit/controllers/subscribe-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { module, test } from 'qunit';
import { setupTest } from 'website-www/tests/helpers';
import Service from '@ember/service';
import { tracked } from '@glimmer/tracking';
class MockLoginService extends Service {
@tracked isLoggedIn = false;
}

module('Unit | Controller | Subscribe', function (hooks) {
setupTest(hooks);

hooks.beforeEach(function () {
this.owner.register('service:login', MockLoginService);
});

test('it exists', function (assert) {
let controller = this.owner.lookup('controller:subscribe');
assert.ok(controller);
});

test('it correctly identifies when a user is logged in', function (assert) {
let controller = this.owner.lookup('controller:join');
let loginService = this.owner.lookup('service:login');

loginService.isLoggedIn = false;
loginService.userData = null;
assert.notOk(
controller.isLoggedIn,
'isLoggedIn is false when user is not logged in',
);

loginService.isLoggedIn = true;
loginService.userData = null;
assert.notOk(
controller.isLoggedIn,
'isLoggedIn is false when userData is null',
);

loginService.isLoggedIn = true;
loginService.userData = { id: '123', username: 'testuser' };
assert.ok(
controller.isLoggedIn,
'isLoggedIn is true when user is logged in and userData is present',
);
});
});

0 comments on commit 2417931

Please sign in to comment.