Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suscription feature testcases #945

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}$/;
Comment on lines +1 to +2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @tejaskh3, I see we are not using any regex in tests. and that too from this file.

Do we need it?

91 changes: 91 additions & 0 deletions tests/integration/components/subscribe-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
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) {
Comment on lines +6 to +9
Copy link
Member

@MehulKChaudhari MehulKChaudhari Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that you are writing an integration test for template/subscription.hbs

For template files like this, we typically write acceptance tests, not integration tests. This is because we want to simulate the full user experience by testing what the user sees when they visit the /subscription (in this case) route and how the application responds to user interactions. Acceptance tests cover aspects like navigating between routes, verifying that the correct content is displayed, and checking how user actions, such as clicks, affect the current route or trigger redirection.

Integration tests are used to test individual components to ensure they render correctly and behave as expected in isolation, including how they respond to different inputs.

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',
);
});
});
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',
);
});
});
Loading