Skip to content

Commit

Permalink
Merge pull request #219 from dancier/add-chat-tests
Browse files Browse the repository at this point in the history
add some first tests to chat
  • Loading branch information
halbekanne committed Jan 12, 2024
2 parents 9bb580c + 69d4822 commit 95acc5a
Show file tree
Hide file tree
Showing 12 changed files with 430 additions and 110 deletions.
11 changes: 6 additions & 5 deletions cypress.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { defineConfig } from "cypress";
import { defineConfig } from 'cypress';

export default defineConfig({
e2e: {
baseUrl: "http://localhost:4200",
baseUrl: 'https://localhost:4200',
testIsolation: true,
// use 'macbook-13' viewport profile per default
viewportWidth: 1280,
viewportHeight: 800,
Expand All @@ -13,9 +14,9 @@ export default defineConfig({

component: {
devServer: {
framework: "angular",
bundler: "webpack",
framework: 'angular',
bundler: 'webpack',
},
specPattern: "**/*.cy.ts",
specPattern: '**/*.cy.ts',
},
});
51 changes: 51 additions & 0 deletions cypress/e2e/chat.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
describe('Chat', () => {
beforeEach(() => {
cy.setupRequestMocking();
cy.login();
});

it('shows "no chats" hint when there are no chats', () => {
cy.intercept('GET', '/chats', { fixture: 'chat/get-chats/0-chats.json' });
cy.visit('/chat');
cy.findByText(/Du hast noch keine Chats/i).should('be.visible');
});

describe('when prompted to open a chat with a specific user', () => {
it('creates and opens a new chat when it doesnt exist', () => {
let chatCreated = false;
cy.intercept('GET', '/chats', (req) => {
if (!chatCreated) {
req.reply({ fixture: 'chat/get-chats/0-chats.json' });
} else {
req.reply({ fixture: 'chat/get-chats/1-chats.json' });
}
});
cy.intercept('POST', 'https://test-dancer.dancier.net/dancers', {
fixture: 'chat/post-dancers/all-details.json',
});
cy.intercept('POST', '/chats', (req) => {
chatCreated = true;
req.reply({ id: '1' });
});
cy.intercept('GET', '/chats/1/messages', []);

cy.visit('/chat?participantId=1');
cy.findByTestId('chat-list-selected-entry')
.should('be.visible')
.should('include.text', 'Username 1');
});

it('opens a previously created chat', () => {
cy.intercept('GET', '/chats', { fixture: 'chat/get-chats/1-chats.json' });
cy.intercept('GET', '/chats/1/messages', []);
cy.intercept('POST', 'https://test-dancer.dancier.net/dancers', {
fixture: 'chat/post-dancers/all-details.json',
});

cy.visit('/chat?participantId=1');
cy.findByTestId('chat-list-selected-entry')
.should('be.visible')
.should('include.text', 'Username 1');
});
});
});
1 change: 1 addition & 0 deletions cypress/fixtures/chat/get-chats/0-chats.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
12 changes: 12 additions & 0 deletions cypress/fixtures/chat/get-chats/1-chats.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"chatId": "1",
"participantIds": [
"0",
"1"
],
"lastActivity": "2023-12-24T11:04:27.293757Z",
"lastMessage": null,
"createdAt": "2023-12-17T20:18:19.621132Z"
}
]
27 changes: 27 additions & 0 deletions cypress/fixtures/chat/get-profile/full-profile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"id": "0",
"size": 110,
"gender": "MALE",
"dancerName": "1001-test",
"birthDate": "1981-02-01",
"ableTo": [
{
"dance": "Standard",
"level": "BASIC",
"leading": "LEAD"
}
],
"wantsTo": [
{
"dance": "Standard",
"level": "NO_EXPERIENCE",
"leading": "FOLLOW"
}
],
"email": "1001-test@dancier.net",
"zipCode": "89188",
"city": "Merklingen",
"country": "GER",
"profileImageHash": "9e82d8b082863e4707d17810c760175e9f93a9a254d2521d3b26d063fe0a7750",
"aboutMe": "Hallo, ich bin der Dominik. Ich spiele gerne Karten und mache auch sonst allerlei Sachen. Manchmal trinke ich Kaffee, aber nur am Wochenende, meistens trinke ich Tee, damit das klar ist. Jojo, das ist töfte, oder?\n\nBla blub, es ist cool ich zu sein. Nein wirklich, es ist super. Jemand anderes zu sen"
}
14 changes: 14 additions & 0 deletions cypress/fixtures/chat/post-dancers/all-details.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"0": {
"id": "0",
"dancerName": "Username 0",
"city": "Stadt 0",
"profileImageHash": null
},
"1": {
"id": "1",
"dancerName": "Username 1",
"city": "Stadt 1",
"profileImageHash": null
}
}
14 changes: 14 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,17 @@
// }
// }
// }

// declare global {
// namespace Cypress {
// interface Chainable {
// login(): Chainable<void>
// }
// }
// }
//
// Cypress.Commands.add('loginLocalStorage', () => {
// window.localStorage.setItem('jwt', resp.body.user.token)
// });

import '@testing-library/cypress/add-commands';
28 changes: 28 additions & 0 deletions cypress/support/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,33 @@ import './commands';

setLightTheme();

declare global {
namespace Cypress {
interface Chainable {
login(): Chainable<void>;
setupRequestMocking(): Chainable<void>;
}
}
}

Cypress.Commands.add('login', () => {
window.localStorage.setItem(
'authData',
JSON.stringify({
jwt: '',
isLoggedIn: true,
isHuman: true,
})
);

cy.intercept('GET', '/profile', {
fixture: 'chat/get-profile/full-profile.json',
});
});

Cypress.Commands.add('setupRequestMocking', () => {
cy.intercept('POST', '/eventlog', {});
});

// Alternatively you can use CommonJS syntax:
// require('./commands')
3 changes: 2 additions & 1 deletion cypress/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"extends": "../tsconfig.json",
"compilerOptions": {
"noEmit": true,
"types": ["cypress"]
"types": ["cypress", "@testing-library/cypress"],
"sourceMap": false
},
"files": ["../cypress.config.ts"],
"include": ["../node_modules/cypress", "./**/*.ts"],
Expand Down
Loading

0 comments on commit 95acc5a

Please sign in to comment.