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

feat: add unit tests for campaigns folder #1724

Merged
merged 48 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
563ac08
unit tests for campaigns folder
reachaadrika May 29, 2023
7ee04b5
other changes to the code , name fix and fixing eslint errors
reachaadrika May 29, 2023
510ae01
Merge branch 'master' into campaignTests
reachaadrika May 29, 2023
c6a39a5
Merge branch 'master' into campaignTests
reachaadrika May 30, 2023
7eab794
Merge branch 'master' into campaignTests
reachaadrika Jun 1, 2023
13e87e6
Merge branch 'master' into campaignTests
reachaadrika Jun 3, 2023
418c7fa
changes in unit tests
reachaadrika Jun 3, 2023
d514196
git
reachaadrika Jun 3, 2023
91585c2
Merge branch 'master' into campaignTests
reachaadrika Jun 6, 2023
d796bcc
Merge branch 'master' of https://github.com/reachaadrika/website into…
reachaadrika Jun 7, 2023
f8c3af6
adding data-cy attribute
reachaadrika Jun 7, 2023
7cf46cc
Merge branch 'campaignTests' of https://github.com/reachaadrika/websi…
reachaadrika Jun 7, 2023
9132a76
Merge branch 'master' into campaignTests
reachaadrika Jun 8, 2023
697d573
final change , replace -cy with -test-id to maintain consistency
reachaadrika Jun 8, 2023
58f092e
Merge branch 'master' into campaignTests
reachaadrika Jun 10, 2023
26b16a5
Merge branch 'master' into campaignTests
reachaadrika Jun 11, 2023
45735ee
removed whitespaces , changed names and done other changes]
reachaadrika Jun 11, 2023
0a96798
Merge branch 'master' into campaignTests
reachaadrika Jun 12, 2023
7d2fd02
Merge branch 'master' into campaignTests
reachaadrika Jun 17, 2023
595cc09
change adding testid on reflected component
reachaadrika Jun 17, 2023
2d2e676
Merge branch 'master' into campaignTests
reachaadrika Jun 17, 2023
90443f8
Merge branch 'master' into campaignTests
reachaadrika Jun 17, 2023
6d8ab7d
Update components/campaigns/Banner.js
reachaadrika Jun 17, 2023
b7fad6e
Update components/campaigns/Banner.js
reachaadrika Jun 17, 2023
3764428
Merge branch 'master' into campaignTests
reachaadrika Jun 18, 2023
27f04df
Merge branch 'master' of https://github.com/reachaadrika/website into…
reachaadrika Jun 19, 2023
8bdaeac
Merge branch 'master' into campaignTests
reachaadrika Jun 21, 2023
62934b6
Merge branch 'campaignTests' of https://github.com/reachaadrika/websi…
reachaadrika Jun 22, 2023
933dde2
Merge branch 'master' of https://github.com/reachaadrika/website into…
reachaadrika Jun 22, 2023
ddb3809
Merge branch 'master' into campaignTests
reachaadrika Jun 23, 2023
5900eae
Merge branch 'master' into campaignTests
reachaadrika Jun 27, 2023
5f6d2de
Merge branch 'master' into campaignTests
reachaadrika Jun 30, 2023
dd0b0e4
Merge branch 'master' into campaignTests
reachaadrika Jul 7, 2023
334887f
Merge branch 'master' into campaignTests
reachaadrika Jul 8, 2023
d2b1ab9
fix campaign tests
reachaadrika Jul 9, 2023
3f339e9
Merge branch 'master' into campaignTests
reachaadrika Jul 10, 2023
34f39c8
Merge branch 'master' into campaignTests
reachaadrika Jul 10, 2023
af76387
Merge branch 'master' into campaignTests
reachaadrika Jul 10, 2023
26df0f0
Merge branch 'master' into campaignTests
reachaadrika Jul 11, 2023
017e1f6
Merge branch 'master' into campaignTests
reachaadrika Jul 11, 2023
bcd67f7
fixed tests for Banner
reachaadrika Jul 11, 2023
fcd713f
Merge branch 'campaignTests' of https://github.com/reachaadrika/websi…
reachaadrika Jul 11, 2023
8128550
Merge branch 'master' into campaignTests
reachaadrika Jul 14, 2023
b80e195
Merge branch 'master' into campaignTests
akshatnema Jul 16, 2023
015b510
fix Announcement Hero
reachaadrika Jul 16, 2023
6027dd5
Merge branch 'campaignTests' of https://github.com/reachaadrika/websi…
reachaadrika Jul 16, 2023
5af3471
Format AnnouncementHero.js
akshatnema Jul 16, 2023
2cd75e7
Merge branch 'master' into campaignTests
reachaadrika Jul 16, 2023
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
5 changes: 0 additions & 5 deletions cypress/test/ComponentName.cy.js
Copy link
Member

Choose a reason for hiding this comment

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

This file is already deleted in another PR. It may create issues while merging any of the PRs.

This file was deleted.

73 changes: 73 additions & 0 deletions cypress/test/campaignTests/AnnouncementHero.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import React from 'react';
import { mount } from '@cypress/react';
import AnnouncementHero from '../../../components/campaigns/AnnoucementHero';

describe('AnnouncementHero Component', () => {
it('should render the component when the date is within the valid range', () => {
// Set the current date to May
const mockDate = new Date('2023-05-01T00:00:00Z');
cy.clock(mockDate.getTime());

mount(<AnnouncementHero />);

// Assert that the component is rendered
cy.get('.bg-gray-50').should('exist');
Copy link
Member

@imabp imabp Jun 4, 2023

Choose a reason for hiding this comment

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

We can use, data-componentName, in the original component, and use it in cypress for more accurate testing?
cc: @akshatnema @reachaadrika

For example:

<button
  id="main"
  class="btn btn-large"
  name="submission"
  role="button"
  data-cy="submit"
>
  Submit
</button>

Cypress Code

cy.get('[data-cy="submit"]')

Benefits of using data-ComponentId, it is isolated from all the JS CSS changes, (post css as well) offers more accuracy to our testing.

Refer this Cypres documentation

Copy link
Member

Choose a reason for hiding this comment

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

I think @reachaadrika is using it at many places. Maybe she forgot about it here, but yeah, the test for background color should be there as it tests whether component is rendering it's properties also.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am checking for background and for rest of the components I will add data attribute

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done the suggested changes , do check @imabp

Copy link
Member

@imabp imabp Jun 8, 2023

Choose a reason for hiding this comment

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

cy.get('.bg-gray-50').should('exist');

@reachaadrika still the change is not reflected here
Kindly update

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

});

it('should not render the component when the date is outside the valid range', () => {
// Set the current date to June
const mockDate = new Date('2023-06-01T00:00:00Z');
cy.clock(mockDate.getTime());

mount(<AnnouncementHero />);

// Assert that the component is not rendered
cy.get('.bg-gray-50').should('not.exist');
Copy link
Member

Choose a reason for hiding this comment

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

same here as well, using data-componentName
ref

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure @imabp adding data-cy and I will keep in mind to add data-cy rather than relying on tags and classes

Copy link
Member

Choose a reason for hiding this comment

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

Yes @reachaadrika, please replace the implementation this with data-cy="nameOfTheComponent", and this will be helpful when we check complex properties of an element. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done the suggested changes , do check @imabp

});

it('should display the correct event information', () => {
const mockDate = new Date('2023-05-01T00:00:00Z');
cy.clock(mockDate.getTime());

mount(<AnnouncementHero />);

// Assert the event details
cy.contains('.countdown-text-gradient', 'AsyncAPI Conf on Tour 2023').should('exist');
cy.contains('.countdown-text-gradient', 'Madrid Edition').should('exist');
cy.contains('October, 2023 | Madrid, Spain').should('exist');
cy.contains('Submit a session').should('exist');
});

it('should have a link to submit a session', () => {
const mockDate = new Date('2023-05-01T00:00:00Z');
cy.clock(mockDate.getTime());

mount(<AnnouncementHero />);

// Assert the link
cy.get('a')
.should('have.attr', 'href', 'https://sessionize.com/aacot-madrid/')
.should('have.attr', 'target', '_blank')
.contains('Submit a session');
});

//check if announcement rendered is small or large .

it('should render a small announcement when "small" prop is true', () => {
const mockDate = new Date('2023-05-01T00:00:00Z');
cy.clock(mockDate.getTime());

mount(<AnnouncementHero small />);

cy.get('.bg-gray-50').should('have.class', 'mb-4');
});

it('should render a large announcement when "small" prop is false', () => {
const mockDate = new Date('2023-05-01T00:00:00Z');
cy.clock(mockDate.getTime());

mount(<AnnouncementHero small={false} />);

cy.get('.bg-gray-50').should('have.class', 'mb-12');
});
akshatnema marked this conversation as resolved.
Show resolved Hide resolved
});
38 changes: 38 additions & 0 deletions cypress/test/campaignTests/AnnouncementRemainingDays.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import moment from 'moment';
import { mount } from '@cypress/react';
import AnnouncementRemainingDays from '../../../components/campaigns/AnnouncementRamainingDays';

describe('AnnouncementRemainingDays', () => {
it('displays correct countdown text', () => {
const dateTime = moment().add(2, 'days').toISOString(); // Set the dateTime value as desired
const eventName = 'Your Event'; // Set the eventName as desired

mount(<AnnouncementRemainingDays dateTime={dateTime} eventName={eventName} />);

const date = moment(dateTime);
const now = moment();
const days = date.diff(now, 'days');

let text;
if (days >= 1) {
text = `${days} ${days === 1 ? 'day' : 'days'}`;
} else {
const hours = date.diff(now, 'hours');
if (hours > 1) {
text = 'A few hours';
} else {
const minutes = date.diff(now, 'minutes');
if (minutes > 1) {
text = 'A few minutes';
}
}
}

cy.get('span').should('have.text', `${text} until ${eventName}`);

//check class font-extrabold
cy.get('span').should('have.class', 'font-extrabold');
});
});

/// Make sure to adjust the dateTime and eventName variables as needed for your specific testcase
79 changes: 79 additions & 0 deletions cypress/test/campaignTests/Banner.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import React from 'react';
import { mount } from '@cypress/react';
import Banner from '../../../components/campaigns/Banner';

describe('Banner Component', () => {
it('should render the banner when the date is within the valid range', () => {
const today = new Date();
const day = today.getUTCDate();
const month = today.getUTCMonth();
const year = today.getUTCFullYear();
Copy link
Member

Choose a reason for hiding this comment

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

@reachaadrika as they are related, use parallel assignment.
Better code readability

Suggested change
const day = today.getUTCDate();
const month = today.getUTCMonth();
const year = today.getUTCFullYear();
const [day, month, year] = [today.getUTCDate(), today.getUTCMonth(), today.getUTCFullYear()]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

resolved done


if (year > 2022 || month !== 10 || day < 6) {
mount(<Banner />);
Copy link
Member

Choose a reason for hiding this comment

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

Use beforeEach here to mount the Banner Component

Copy link
Contributor Author

@reachaadrika reachaadrika Jul 16, 2023

Choose a reason for hiding this comment

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

For this I can't use BeforeEach since , for some parts in the code , I am rendering Banner after the clock is set , to get the Banner displayed like here const mockDate = new Date(2021, 10, 12).getTime(); cy.clock(mockDate); mount(<Banner />);

cy.get('.bg-gray-100').should('not.exist');

} else {
mount(<Banner />);

cy.get('.bg-gray-100').should('be.visible');

}
});

it('should display the correct message when the date is within the valid range', () => {
const today = new Date();
const day = today.getUTCDate();
const month = today.getUTCMonth();
const year = today.getUTCFullYear();

if (year > 2022 || month !== 10 || day < 6) {
mount(<Banner />);
cy.contains('.font-medium', 'AsyncAPI Conference 2022 has ended').should('not.exist');
} else {
mount(<Banner />);

cy.contains('.font-medium', 'AsyncAPI Conference 2022 has ended').should('be.visible');
}
});

it('should have a link to the recordings playlist', () => {
const today = new Date();
const day = today.getUTCDate();
const month = today.getUTCMonth();
const year = today.getUTCFullYear();

if (year === 2022 && month === 10 && day >= 6) {
mount(<Banner />);

cy.get('a')
.should('have.attr', 'href', 'https://www.youtube.com/playlist?list=PLbi1gRlP7pijRiA32SU36hD_FW-2qyPhl')
.should('have.attr', 'target', '_blank')
.should('have.attr', 'rel', 'noopener noreferrer');
} else {
mount(<Banner />);

cy.get('a').should('not.exist');
}
});


//check to see if max-w-screen is present or not , for full width element
it('should have the max-w-screen-xl class in the div element', () => {
const today = new Date();
const day = today.getUTCDate();
const month = today.getUTCMonth();
const year = today.getUTCFullYear();
Copy link
Member

Choose a reason for hiding this comment

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

use parallel assignment, as said above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

resolved done


if (year === 2022 && month === 10 && day >= 6) {
mount(<Banner />);
cy.get('.max-w-screen-xl').should('exist');
} else {
mount(<Banner />);
cy.get('.max-w-screen-xl').should('not.exist');
}
});


});