Skip to content

Commit a300884

Browse files
committed
test: add Cypress component tests for MainPage
1 parent 97b4da6 commit a300884

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

cypress.config.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { defineConfig } from 'cypress';
2+
3+
export default defineConfig({
4+
component: {
5+
devServer: {
6+
framework: 'react',
7+
bundler: 'webpack',
8+
},
9+
supportFile: './cypress/support/component.ts',
10+
specPattern: 'cypress/e2e/**/*.cy.{js,jsx,ts,tsx}',
11+
},
12+
e2e: {
13+
baseUrl: 'http://localhost:3000',
14+
supportFile: false,
15+
},
16+
});

cypress/e2e/mainPage.cy.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
describe('MainPage Component', () => {
2+
const testUsername = 'testuser123';
3+
4+
beforeEach(() => {
5+
// Mock the useEntries hook response
6+
cy.intercept('GET', '/api/entries', {
7+
statusCode: 200,
8+
body: {
9+
data: {
10+
username: testUsername,
11+
entries: []
12+
}
13+
}
14+
}).as('getEntries');
15+
16+
cy.mount(<MainPage />);
17+
cy.wait('@getEntries');
18+
});
19+
20+
it('should display the main page container', () => {
21+
cy.get('main').should('exist');
22+
cy.get('main').should('have.class', 'min-h-screen');
23+
});
24+
25+
it('should display the correct username in the header', () => {
26+
cy.get('h1')
27+
.should('contain.text', `Statement builder for ${testUsername}`)
28+
.and('have.class', 'text-3xl');
29+
});
30+
31+
it('should render the StatementList component', () => {
32+
cy.get('.container').should('exist');
33+
cy.get('.container').should('have.class', 'mx-auto');
34+
});
35+
36+
it('should have the correct background gradient', () => {
37+
cy.get('main')
38+
.should('have.class', 'bg-gradient-to-b')
39+
.and('have.class', 'from-gray-50')
40+
.and('have.class', 'to-gray-100');
41+
});
42+
});

cypress/support/component.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { mount } from 'cypress/react18';
2+
import '../../src/index.css';
3+
4+
// Augment the Cypress namespace to include type definitions for
5+
// your custom command.
6+
declare global {
7+
namespace Cypress {
8+
interface Chainable {
9+
mount: typeof mount;
10+
}
11+
}
12+
}
13+
14+
Cypress.Commands.add('mount', mount);

0 commit comments

Comments
 (0)