/**
* Cypress Component Tests for edc-assets-management
*
* Tests the Assets feature component in isolation
*/
import '../../src/components/features/assets/edc-assets-management.ts';
import { html } from 'lit';
import type { EdcAssetsManagement } from '../../src/components/features/assets/edc-assets-management.ts';
import { createEdcStore } from '../../src/services/store-factory';
describe('edc-assets-management', () => {
describe('Component Initialization', () => {
it('should mount the component successfully', () => {
const { store } = createEdcStore({});
cy.mount(html``);
cy.get('edc-assets-management').should('exist');
});
it('should require store property', () => {
const { store } = createEdcStore({});
cy.mount(html``);
cy.get('edc-assets-management')
.shadow()
.find('.assets-management')
.should('exist');
});
it('should load mock assets on initialization', () => {
const { store } = createEdcStore({});
cy.mount(html``);
// Wait for assets to load
cy.get('edc-assets-management')
.shadow()
.find('.asset-card', { timeout: 2000 })
.should('have.length.at.least', 1);
});
});
describe('Asset Display', () => {
beforeEach(() => {
const { store } = createEdcStore({});
cy.mount(html``);
});
it('should display asset cards with proper structure', () => {
cy.get('edc-assets-management')
.shadow()
.find('.asset-card')
.first()
.within(() => {
cy.get('.asset-id').should('exist');
cy.get('.asset-title').should('exist');
cy.get('.asset-description').should('exist');
cy.get('.asset-actions').should('exist');
});
});
it('should display asset ID', () => {
cy.get('edc-assets-management')
.shadow()
.find('.asset-card .asset-id')
.first()
.should('contain.text', 'demo-asset');
});
it('should display Edit and Delete buttons', () => {
cy.get('edc-assets-management')
.shadow()
.find('.asset-card')
.first()
.within(() => {
cy.get('button').contains('Edit').should('exist');
cy.get('button').contains('Delete').should('exist');
});
});
});
describe('Search Functionality', () => {
beforeEach(() => {
const { store } = createEdcStore({});
cy.mount(html``);
});
it('should have a search input', () => {
cy.get('edc-assets-management')
.shadow()
.find('input[placeholder*="Search"]')
.should('exist');
});
it('should filter assets by search term', () => {
// Wait for initial load
cy.get('edc-assets-management')
.shadow()
.find('.asset-card', { timeout: 2000 })
.should('have.length.at.least', 2);
// Search for specific asset
cy.get('edc-assets-management')
.shadow()
.find('input[placeholder*="Search"]')
.type('Demo Asset 1');
// Should filter to 1 result
cy.get('edc-assets-management')
.shadow()
.find('.asset-card')
.should('have.length', 1)
.and('contain', 'Demo Asset 1');
});
it('should show all assets when search is cleared', () => {
// Search
cy.get('edc-assets-management')
.shadow()
.find('input[placeholder*="Search"]')
.type('Demo Asset 1');
cy.get('edc-assets-management')
.shadow()
.find('.asset-card')
.should('have.length', 1);
// Clear search
cy.get('edc-assets-management')
.shadow()
.find('input[placeholder*="Search"]')
.clear();
// Should show all assets again
cy.get('edc-assets-management')
.shadow()
.find('.asset-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-assets-management')
.shadow()
.find('select.sort-select')
.should('exist');
cy.get('edc-assets-management')
.shadow()
.find('button')
.contains('↑')
.should('exist');
});
it('should have sort options', () => {
cy.get('edc-assets-management')
.shadow()
.find('select.sort-select option')
.should('have.length.at.least', 3); // ID, Title, Created
});
it('should toggle sort direction', () => {
cy.get('edc-assets-management')
.shadow()
.find('button')
.contains('↑')
.click();
cy.get('edc-assets-management')
.shadow()
.find('button')
.contains('↓')
.should('exist');
});
});
describe('Create Asset Form', () => {
beforeEach(() => {
const { store } = createEdcStore({});
cy.mount(html``);
});
it('should open create form when Create Asset button clicked', () => {
cy.get('edc-assets-management')
.shadow()
.find('button')
.contains('Create Asset')
.click();
cy.get('edc-assets-management')
.shadow()
.find('.modal-overlay')
.should('exist');
cy.get('edc-assets-management')
.shadow()
.find('.modal-header h3')
.should('contain.text', 'Create New Asset');
});
it('should have required form fields', () => {
cy.get('edc-assets-management')
.shadow()
.find('button')
.contains('Create Asset')
.click();
cy.get('edc-assets-management')
.shadow()
.find('input[name="assetId"]')
.should('exist')
.and('have.attr', 'required');
});
it('should close form when Cancel clicked', () => {
cy.get('edc-assets-management')
.shadow()
.find('button')
.contains('Create Asset')
.click();
cy.get('edc-assets-management')
.shadow()
.find('.modal-overlay')
.should('exist');
cy.get('edc-assets-management')
.shadow()
.find('button')
.contains('Cancel')
.click();
cy.get('edc-assets-management')
.shadow()
.find('.modal-overlay')
.should('not.exist');
});
it('should create a new asset successfully', () => {
// Open form
cy.get('edc-assets-management')
.shadow()
.find('button')
.contains('Create Asset')
.click();
// Fill form
cy.get('edc-assets-management')
.shadow()
.find('input[name="assetId"]')
.type('test-asset-cypress', { force: true });
cy.get('edc-assets-management')
.shadow()
.find('input[name="title"]')
.type('Test Asset', { force: true });
cy.get('edc-assets-management')
.shadow()
.find('textarea[name="description"]')
.type('Test asset created by Cypress', { force: true });
// Submit
cy.get('edc-assets-management')
.shadow()
.find('button[type="submit"]')
.click({ force: true });
// Wait for operation
cy.wait(600);
// Verify form closed
cy.get('edc-assets-management')
.shadow()
.find('.modal-overlay')
.should('not.exist');
// Verify asset appears
cy.get('edc-assets-management')
.shadow()
.find('.asset-card')
.contains('test-asset-cypress')
.should('exist');
});
});
describe('Edit Asset', () => {
beforeEach(() => {
const { store } = createEdcStore({});
cy.mount(html``);
});
it('should open edit form when Edit button clicked', () => {
cy.get('edc-assets-management')
.shadow()
.find('.asset-card')
.first()
.find('button')
.contains('Edit')
.click();
cy.get('edc-assets-management')
.shadow()
.find('.modal-overlay')
.should('exist');
cy.get('edc-assets-management')
.shadow()
.find('.modal-header h3')
.should('contain.text', 'Edit Asset');
});
it('should pre-populate form with asset data', () => {
cy.get('edc-assets-management')
.shadow()
.find('.asset-card')
.first()
.find('button')
.contains('Edit')
.click();
// Wait for modal to appear
cy.get('edc-assets-management')
.shadow()
.find('.modal-content', { timeout: 2000 })
.should('exist');
cy.get('edc-assets-management')
.shadow()
.find('.modal-content input')
.first()
.should('be.disabled');
});
});
describe('Delete Asset', () => {
beforeEach(() => {
const { store } = createEdcStore({});
cy.mount(html``);
});
it('should delete asset when confirmed', () => {
// Get initial count
cy.get('edc-assets-management')
.shadow()
.find('.asset-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-assets-management')
.shadow()
.find('.asset-card')
.first()
.find('button')
.contains('Delete')
.click({ force: true });
// Wait for deletion
cy.wait(600);
// Verify count decreased
cy.get('edc-assets-management')
.shadow()
.find('.asset-card')
.should('have.length', initialCount - 1);
});
});
it('should not delete asset when cancelled', () => {
// Get initial count
cy.get('edc-assets-management')
.shadow()
.find('.asset-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-assets-management')
.shadow()
.find('.asset-card')
.first()
.find('button')
.contains('Delete')
.click({ force: true });
// Verify count unchanged
cy.get('edc-assets-management')
.shadow()
.find('.asset-card')
.should('have.length', initialCount);
});
});
});
describe('Refresh Functionality', () => {
beforeEach(() => {
const { store } = createEdcStore({});
cy.mount(html``);
});
it('should have a refresh button', () => {
cy.get('edc-assets-management')
.shadow()
.find('button')
.contains('Refresh')
.should('exist');
});
it('should reload assets when refresh clicked', () => {
cy.get('edc-assets-management')
.shadow()
.find('button')
.contains('Refresh')
.click();
// Should show assets (reloaded from mock store)
cy.get('edc-assets-management')
.shadow()
.find('.asset-card', { timeout: 2000 })
.should('have.length.at.least', 1);
});
});
describe('Empty State', () => {
it('should show empty state when no assets match search', () => {
const { store } = createEdcStore({});
cy.mount(html``);
// Search for non-existent asset
cy.get('edc-assets-management')
.shadow()
.find('input[placeholder*="Search"]')
.type('nonexistent-asset-xyz');
// Should show empty state
cy.get('edc-assets-management')
.shadow()
.find('.empty-state')
.should('exist')
.and('contain.text', 'No assets found');
});
});
describe('Event Emission', () => {
it('should emit asset-created event when asset is created', (done) => {
const { store } = createEdcStore({});
cy.mount(html``).then((component) => {
const el = component[0] as unknown as EdcAssetsManagement;
// Listen for event
el.addEventListener('asset-created', (e: Event) => {
expect(e).to.exist;
done();
});
// Create asset
cy.get('edc-assets-management')
.shadow()
.find('button')
.contains('Create Asset')
.click();
cy.get('edc-assets-management')
.shadow()
.find('input[name="assetId"]')
.type('event-test', { force: true });
cy.get('edc-assets-management')
.shadow()
.find('button[type="submit"]')
.click({ force: true });
});
});
});
});