/**
* Cypress Component Tests for edc-policies-management
*
* Tests the Policies feature component in isolation
*/
import '../../src/components/features/policies/edc-policies-management.ts';
import { html } from 'lit';
import type { EdcPoliciesManagement } from '../../src/components/features/policies/edc-policies-management.ts';
import { createEdcStore } from '../../src/services/store-factory';
describe('edc-policies-management', () => {
describe('Component Initialization', () => {
it('should mount the component successfully', () => {
const { store } = createEdcStore({});
cy.mount(html``);
cy.get('edc-policies-management').should('exist');
});
it('should require store property', () => {
const { store } = createEdcStore({});
cy.mount(html``);
cy.get('edc-policies-management')
.shadow()
.find('.policies-management')
.should('exist');
});
it('should load mock policies on initialization', () => {
const { store } = createEdcStore({});
cy.mount(html``);
// Wait for policies to load
cy.get('edc-policies-management')
.shadow()
.find('.policy-card', { timeout: 2000 })
.should('have.length.at.least', 1);
});
});
describe('Policy Display', () => {
beforeEach(() => {
const { store } = createEdcStore({});
cy.mount(html``);
});
it('should display policy cards with proper structure', () => {
cy.get('edc-policies-management')
.shadow()
.find('.policy-card')
.first()
.within(() => {
cy.get('.policy-id').should('exist');
cy.get('.policy-type').should('exist');
cy.get('.policy-actions').should('exist');
});
});
it('should display policy ID', () => {
cy.get('edc-policies-management')
.shadow()
.find('.policy-card .policy-id')
.first()
.should('contain.text', 'allow-all-policy');
});
it('should display policy type', () => {
cy.get('edc-policies-management')
.shadow()
.find('.policy-card .policy-type')
.first()
.should('contain.text', 'Set');
});
it('should display Edit and Delete buttons', () => {
cy.get('edc-policies-management')
.shadow()
.find('.policy-card')
.first()
.within(() => {
cy.get('button').contains('Edit').should('exist');
cy.get('button').contains('Delete').should('exist');
});
});
it('should display permissions section', () => {
cy.get('edc-policies-management')
.shadow()
.find('.policy-card')
.first()
.find('.policy-section')
.should('contain.text', 'Permissions');
});
});
describe('Search Functionality', () => {
beforeEach(() => {
const { store } = createEdcStore({});
cy.mount(html``);
});
it('should have a search input', () => {
cy.get('edc-policies-management')
.shadow()
.find('input[placeholder*="Search"]')
.should('exist');
});
it('should filter policies by search term', () => {
// Wait for initial load
cy.get('edc-policies-management')
.shadow()
.find('.policy-card', { timeout: 2000 })
.should('have.length.at.least', 2);
// Search for specific policy
cy.get('edc-policies-management')
.shadow()
.find('input[placeholder*="Search"]')
.type('allow-all');
// Should filter to 1 result
cy.get('edc-policies-management')
.shadow()
.find('.policy-card')
.should('have.length', 1)
.and('contain', 'allow-all');
});
it('should show all policies when search is cleared', () => {
// Search
cy.get('edc-policies-management')
.shadow()
.find('input[placeholder*="Search"]')
.type('allow-all');
cy.get('edc-policies-management')
.shadow()
.find('.policy-card')
.should('have.length', 1);
// Clear search
cy.get('edc-policies-management')
.shadow()
.find('input[placeholder*="Search"]')
.clear();
// Should show all policies again
cy.get('edc-policies-management')
.shadow()
.find('.policy-card')
.should('have.length.at.least', 2);
});
});
describe('Sort Functionality', () => {
beforeEach(() => {
const { store } = createEdcStore({});
cy.mount(html``);
});
it('should have sort controls', () => {
cy.get('edc-policies-management')
.shadow()
.find('select.sort-select')
.should('exist');
cy.get('edc-policies-management')
.shadow()
.find('button')
.contains('↑')
.should('exist');
});
it('should have sort options', () => {
cy.get('edc-policies-management')
.shadow()
.find('select.sort-select option')
.should('have.length.at.least', 3); // ID, Type, Created
});
it('should toggle sort direction', () => {
cy.get('edc-policies-management')
.shadow()
.find('button')
.contains('↑')
.click();
cy.get('edc-policies-management')
.shadow()
.find('button')
.contains('↓')
.should('exist');
});
});
describe('Create Policy Form', () => {
beforeEach(() => {
const { store } = createEdcStore({});
cy.mount(html``);
});
it('should open create form when Create Policy button clicked', () => {
cy.get('edc-policies-management')
.shadow()
.find('button')
.contains('Create Policy')
.click();
cy.get('edc-policies-management')
.shadow()
.find('.modal-overlay')
.should('exist');
cy.get('edc-policies-management')
.shadow()
.find('.modal-header h3')
.should('contain.text', 'Create New ODRL Policy');
});
it('should have required form fields', () => {
cy.get('edc-policies-management')
.shadow()
.find('button')
.contains('Create Policy')
.click();
cy.get('edc-policies-management')
.shadow()
.find('input[name="policyId"]')
.should('exist')
.and('have.attr', 'required');
cy.get('edc-policies-management')
.shadow()
.find('select[name="policyType"]')
.should('exist')
.and('have.attr', 'required');
});
it('should have policy type options', () => {
cy.get('edc-policies-management')
.shadow()
.find('button')
.contains('Create Policy')
.click();
cy.get('edc-policies-management')
.shadow()
.find('select[name="policyType"] option')
.should('have.length', 3); // Set, Offer, Agreement
});
it('should close form when Cancel clicked', () => {
cy.get('edc-policies-management')
.shadow()
.find('button')
.contains('Create Policy')
.click();
cy.get('edc-policies-management')
.shadow()
.find('.modal-overlay')
.should('exist');
cy.get('edc-policies-management')
.shadow()
.find('button')
.contains('Cancel')
.click();
cy.get('edc-policies-management')
.shadow()
.find('.modal-overlay')
.should('not.exist');
});
it('should create a new policy successfully', () => {
// Open form
cy.get('edc-policies-management')
.shadow()
.find('button')
.contains('Create Policy')
.click();
// Fill form
cy.get('edc-policies-management')
.shadow()
.find('input[name="policyId"]')
.type('test-policy-cypress', { force: true });
cy.get('edc-policies-management')
.shadow()
.find('select[name="policyType"]')
.select('Set', { force: true });
cy.get('edc-policies-management')
.shadow()
.find('input[name="permissionAction"]')
.clear({ force: true })
.type('USE', { force: true });
// Submit
cy.get('edc-policies-management')
.shadow()
.find('button[type="submit"]')
.click({ force: true });
// Wait for operation
cy.wait(600);
// Verify form closed
cy.get('edc-policies-management')
.shadow()
.find('.modal-overlay')
.should('not.exist');
// Verify policy appears
cy.get('edc-policies-management')
.shadow()
.find('.policy-card')
.contains('test-policy-cypress')
.should('exist');
});
});
describe('Edit Policy', () => {
beforeEach(() => {
const { store } = createEdcStore({});
cy.mount(html``);
});
it('should open edit form when Edit button clicked', () => {
cy.get('edc-policies-management')
.shadow()
.find('.policy-card')
.first()
.find('button')
.contains('Edit')
.click();
cy.get('edc-policies-management')
.shadow()
.find('.modal-overlay')
.should('exist');
cy.get('edc-policies-management')
.shadow()
.find('.modal-header h3')
.should('contain.text', 'Edit Policy');
});
it('should pre-populate form with policy data', () => {
cy.get('edc-policies-management')
.shadow()
.find('.policy-card')
.first()
.find('button')
.contains('Edit')
.click();
// Wait for modal to appear
cy.get('edc-policies-management')
.shadow()
.find('.modal-content', { timeout: 2000 })
.should('exist');
cy.get('edc-policies-management')
.shadow()
.find('.modal-content input')
.first()
.should('be.disabled');
});
});
describe('Delete Policy', () => {
beforeEach(() => {
const { store } = createEdcStore({});
cy.mount(html``);
});
it('should delete policy when confirmed', () => {
// Get initial count
cy.get('edc-policies-management')
.shadow()
.find('.policy-card')
.its('length')
.then((initialCount) => {
// Stub confirm to return true
cy.window().then((win) => {
cy.stub(win, 'confirm').returns(true);
});
// Click delete
cy.get('edc-policies-management')
.shadow()
.find('.policy-card')
.first()
.find('button')
.contains('Delete')
.click({ force: true });
// Wait for deletion
cy.wait(600);
// Verify count decreased
cy.get('edc-policies-management')
.shadow()
.find('.policy-card')
.should('have.length', initialCount - 1);
});
});
it('should not delete policy when cancelled', () => {
// Get initial count
cy.get('edc-policies-management')
.shadow()
.find('.policy-card')
.its('length')
.then((initialCount) => {
// Stub confirm to return false
cy.window().then((win) => {
cy.stub(win, 'confirm').returns(false);
});
// Click delete
cy.get('edc-policies-management')
.shadow()
.find('.policy-card')
.first()
.find('button')
.contains('Delete')
.click({ force: true });
// Verify count unchanged
cy.get('edc-policies-management')
.shadow()
.find('.policy-card')
.should('have.length', initialCount);
});
});
});
describe('Refresh Functionality', () => {
beforeEach(() => {
const { store } = createEdcStore({});
cy.mount(html``);
});
it('should have a refresh button', () => {
cy.get('edc-policies-management')
.shadow()
.find('button')
.contains('Refresh')
.should('exist');
});
it('should reload policies when refresh clicked', () => {
cy.get('edc-policies-management')
.shadow()
.find('button')
.contains('Refresh')
.click();
// Should show policies (reloaded from mock store)
cy.get('edc-policies-management')
.shadow()
.find('.policy-card', { timeout: 2000 })
.should('have.length.at.least', 1);
});
});
describe('Empty State', () => {
it('should show empty state when no policies match search', () => {
const { store } = createEdcStore({});
cy.mount(html``);
// Search for non-existent policy
cy.get('edc-policies-management')
.shadow()
.find('input[placeholder*="Search"]')
.type('nonexistent-policy-xyz');
// Should show empty state
cy.get('edc-policies-management')
.shadow()
.find('.empty-state')
.should('exist')
.and('contain.text', 'No policies found');
});
});
describe('Policy Details Display', () => {
beforeEach(() => {
const { store } = createEdcStore({});
cy.mount(html``);
});
it('should display policy with constraints', () => {
// Find the restricted policy (has constraints)
cy.get('edc-policies-management')
.shadow()
.find('.policy-card')
.contains('restricted-policy')
.parent('.policy-card')
.within(() => {
cy.get('.policy-section').should('contain.text', 'Permissions');
cy.get('.constraints').should('exist');
});
});
it('should display constraint details', () => {
cy.get('edc-policies-management')
.shadow()
.find('.policy-card')
.contains('restricted-policy')
.parent('.policy-card')
.within(() => {
cy.get('.constraint')
.should('exist')
.and('contain.text', 'PURPOSE')
.and('contain.text', 'EQ')
.and('contain.text', 'research');
});
});
});
describe('Event Emission', () => {
it('should emit policy-created event when policy is created', (done) => {
const { store } = createEdcStore({});
cy.mount(html``).then((component) => {
const el = component[0] as unknown as EdcPoliciesManagement;
// Listen for event
el.addEventListener('policy-created', (e: Event) => {
expect(e).to.exist;
done();
});
// Create policy
cy.get('edc-policies-management')
.shadow()
.find('button')
.contains('Create Policy')
.click();
cy.get('edc-policies-management')
.shadow()
.find('input[name="policyId"]')
.type('event-test', { force: true });
cy.get('edc-policies-management')
.shadow()
.find('button[type="submit"]')
.click({ force: true });
});
});
});
});