Files
timmal/features/step_definitions/page-title.steps.mjs

99 lines
2.7 KiB
JavaScript
Raw Normal View History

2025-12-07 21:27:23 +01:00
import { Given, When, Then, Before } from '@cucumber/cucumber';
import { expect } from 'chai';
// Shared state (simulating useState behavior)
const sharedState = { pageName: '' };
// Simple mock of usePageTitle behavior for Cucumber tests
// This simulates the useState() sharing behavior
const createPageTitleMock = () => {
return {
get title() {
return {
get value() {
return sharedState.pageName.length > 0 ? `${sharedState.pageName} - Tímmál` : 'Tímmál';
},
};
},
get pageName() {
return {
get value() {
return sharedState.pageName;
},
};
},
setPageName(newName) {
sharedState.pageName = newName;
},
};
};
// Instance for step definitions
let pageTitleInstance;
const userInstances = new Map();
Before(function () {
// Reset shared state before each scenario
sharedState.pageName = '';
userInstances.clear();
pageTitleInstance = null;
});
Given('the page title system is initialized', function () {
pageTitleInstance = createPageTitleMock();
pageTitleInstance.setPageName('');
});
When('I first load the application', function () {
pageTitleInstance = createPageTitleMock();
});
When('I navigate to the {string} page', function (pageName) {
if (!pageTitleInstance) {
pageTitleInstance = createPageTitleMock();
}
pageTitleInstance.setPageName(pageName);
});
When('I clear the page name', function () {
pageTitleInstance.setPageName('');
});
Given('I am on the {string} page', function (pageName) {
if (!pageTitleInstance) {
pageTitleInstance = createPageTitleMock();
}
pageTitleInstance.setPageName(pageName);
});
Given('the page title is {string}', function (expectedTitle) {
expect(pageTitleInstance.title.value).to.equal(expectedTitle);
});
Given('user {string} sets the page name to {string}', function (userName, pageName) {
let userInstance = userInstances.get(userName);
if (!userInstance) {
userInstance = createPageTitleMock();
userInstances.set(userName, userInstance);
}
userInstance.setPageName(pageName);
});
When('user {string} checks the page title', function (userName) {
let userInstance = userInstances.get(userName);
if (!userInstance) {
userInstance = createPageTitleMock();
userInstances.set(userName, userInstance);
}
});
Then('the page title should be {string}', function (expectedTitle) {
expect(pageTitleInstance.title.value).to.equal(expectedTitle);
});
Then('user {string} should see {string}', function (userName, expectedTitle) {
const userInstance = userInstances.get(userName);
expect(userInstance).to.not.be.undefined;
expect(userInstance.title.value).to.equal(expectedTitle);
});