-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from chiamakaikeanyi/cf-020
Add tests
- Loading branch information
Showing
15 changed files
with
259 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,20 @@ | ||
import { defineConfig } from "cypress"; | ||
|
||
export default defineConfig({ | ||
defaultCommandTimeout: 5000, | ||
pageLoadTimeout: 10000, | ||
|
||
component: { | ||
devServer: { | ||
framework: "create-react-app", | ||
bundler: "webpack", | ||
}, | ||
}, | ||
|
||
e2e: { | ||
baseUrl: "http://localhost:3000", | ||
setupNodeEvents(on, config) { | ||
// implement node event listeners here | ||
}, | ||
}, | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/// <reference types="cypress" /> | ||
|
||
const selectors = { | ||
theme_switch: '[data-testid="theme_switch"]', | ||
app_container: '[data-testid="app_container"]', | ||
home_container: '[data-testid="home_container"]', | ||
country_search: '[data-testid="country_search"]', | ||
regions_filter: '[data-testid="regions"]', | ||
countries_list: '[data-testid="countries_list"]', | ||
details_container: '[data-testid="details_container"]', | ||
back_button: '[data-testid="back_button"]', | ||
border_countries: '[data-testid="border_countries"]', | ||
border_countries_list: '[data-testid="border_countries_list"]', | ||
empty_state_container: '[data-testid="empty_state_container"]', | ||
}; | ||
|
||
describe("App", () => { | ||
beforeEach(() => { | ||
cy.visit("/"); | ||
}); | ||
|
||
it("renders without crashing", () => { | ||
cy.get(selectors.app_container).should("exist"); | ||
}); | ||
|
||
it("toggles the theme", () => { | ||
cy.get(selectors.theme_switch).click(); | ||
cy.get('[aria-label="Switch to dark mode"]').should("exist"); | ||
}); | ||
|
||
it("displays the search input and region filter", () => { | ||
cy.get(selectors.country_search).should("exist"); | ||
cy.get(selectors.regions_filter).should("exist"); | ||
}); | ||
|
||
it("fetches and displays countries", () => { | ||
cy.intercept("GET", "/v3.1/*").as("getData"); | ||
|
||
cy.wait("@getData"); | ||
|
||
cy.get(selectors.countries_list).should("have.length.above", 0); | ||
}); | ||
|
||
it("searches for a country", () => { | ||
cy.get(selectors.country_search).type("Germany"); | ||
cy.get(selectors.home_container).contains("Germany").should("be.visible"); | ||
|
||
cy.contains("Countries in the world - 1 country").should("be.visible"); | ||
}); | ||
|
||
it("filters countries by region", () => { | ||
cy.get(selectors.regions_filter).select("Europe"); | ||
cy.get(selectors.home_container).contains("Europe").should("be.visible"); | ||
}); | ||
|
||
it("navigates to country details when a card is clicked", () => { | ||
cy.get('[data-testid="deu"]').click(); | ||
cy.url().should("include", "/deu"); | ||
cy.get(selectors.details_container).should("exist"); | ||
cy.get(selectors.border_countries).should("exist"); | ||
}); | ||
|
||
it("navigates to the first border country when clicked", () => { | ||
cy.get('[data-testid="deu"]').click(); | ||
cy.url().should("include", "/deu"); | ||
cy.get(selectors.details_container).should("exist"); | ||
cy.get(selectors.border_countries).should("exist"); | ||
|
||
cy.get(selectors.border_countries_list).find("li").first().click(); | ||
cy.url().should("include", "/nld"); | ||
|
||
// navigates back when the Back button is clicked | ||
cy.get(selectors.back_button).click(); | ||
cy.url().should("contain", "/deu"); | ||
}); | ||
}); | ||
|
||
describe("Error", () => { | ||
it("renders error page without crashing", () => { | ||
cy.visit("/deut"); | ||
cy.get(selectors.empty_state_container).should("exist"); | ||
|
||
cy.wait(Cypress.config("pageLoadTimeout")); | ||
cy.contains("An error occured. Please try again.").should("be.visible"); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React from "react"; | ||
import { mount } from "cypress/react18"; | ||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
import { MemoryRouter, Routes, Route } from "react-router-dom"; | ||
import Details from "../../src/pages/Details/Details"; | ||
|
||
const selectors = { | ||
details_container: '[data-testid="details_container"]', | ||
}; | ||
|
||
describe("Details", () => { | ||
let queryClient; | ||
|
||
beforeEach(() => { | ||
queryClient = new QueryClient(); | ||
|
||
mount( | ||
<QueryClientProvider client={queryClient}> | ||
<MemoryRouter initialEntries={["/deu"]}> | ||
<Routes> | ||
<Route path=":countryCode" element={<Details />} /> | ||
</Routes> | ||
</MemoryRouter> | ||
</QueryClientProvider> | ||
); | ||
}); | ||
|
||
it("renders without crashing", () => { | ||
cy.get(selectors.details_container).should("exist"); | ||
}); | ||
|
||
it("displays country details correctly", () => { | ||
cy.get('[data-testid="deu"]') | ||
.scrollIntoView(); | ||
cy.contains("Germany").should("be.visible"); | ||
cy.contains("Native Name:").should("be.visible"); | ||
cy.contains("Population").should("be.visible"); | ||
cy.contains("Region").should("be.visible"); | ||
cy.contains("Sub Region").should("be.visible"); | ||
cy.contains("Capital").should("be.visible"); | ||
cy.contains("Top Level Domain").should("be.visible"); | ||
cy.contains("Currencies").should("be.visible"); | ||
cy.contains("Languages").should("be.visible"); | ||
cy.contains("Driver's Side").should("be.visible"); | ||
cy.contains("Timezone(s)").should("be.visible"); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,40 @@ | ||
import React from "react"; | ||
import { mount } from "cypress/react18"; | ||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
import { MemoryRouter } from "react-router-dom"; | ||
import Home from "../../src/pages/Home/Home"; | ||
|
||
const selectors = { | ||
container: "[data-testid='home_container']", | ||
home_container: "[data-testid='home_container']", | ||
}; | ||
|
||
describe("Home", () => { | ||
it("renders", () => { | ||
// see: https://on.cypress.io/mounting-react | ||
mount(<Home />); | ||
cy.get(selectors.container).should("be.visible"); | ||
let queryClient; | ||
|
||
beforeEach(() => { | ||
queryClient = new QueryClient(); | ||
mount( | ||
<QueryClientProvider client={queryClient}> | ||
<MemoryRouter> | ||
<Home /> | ||
</MemoryRouter> | ||
</QueryClientProvider> | ||
); | ||
}); | ||
|
||
it("renders without crashing", () => { | ||
cy.get(selectors.home_container).should("exist"); | ||
cy.contains("Countries in the world - More than 200 countries").should( | ||
"be.visible" | ||
); | ||
}); | ||
|
||
it("displays the countries", () => { | ||
cy.get('[data-testid="deu"]') | ||
.scrollIntoView(); | ||
cy.contains("Germany").should("be.visible"); | ||
cy.contains("Population: 83,240,525").should("be.visible"); | ||
cy.contains("Region: Europe").should("be.visible"); | ||
cy.contains("Capital: Berlin").should("be.visible"); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// *********************************************************** | ||
// This example support/e2e.ts is processed and | ||
// loaded automatically before your test files. | ||
// | ||
// This is a great place to put global configuration and | ||
// behavior that modifies Cypress. | ||
// | ||
// You can change the location of this file or turn off | ||
// automatically serving support files with the | ||
// 'supportFile' configuration option. | ||
// | ||
// You can read more here: | ||
// https://on.cypress.io/configuration | ||
// *********************************************************** | ||
|
||
// Import commands.js using ES2015 syntax: | ||
import './commands' | ||
|
||
// Alternatively you can use CommonJS syntax: | ||
// require('./commands') |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.