context('SACDS', () => {
  beforeEach(() => {
    cy.visit('http://localhost:3000/')
  })

  describe('Page display', () => {

    it('Displays Incommon logo', () => {
      cy.get('img')
      .should('be.visible')
      .should('have.class', 'logo')
    })

    it('Displays information links', () => {
      cy.get('#menu').should('be.visible');
  
      // Check each link within the menu
      cy.get('#menu a').each(($link) => {
        cy.wrap($link).should('have.attr', 'target', '_blank'); // Check if the link opens in a new tab
      });
  
      // Check the specific attributes of the second and third links
      cy.get('#menu a').eq(1).should('have.attr', 'href', 'https://internet2.edu/community/about-us/policies/privacy/').and('have.class', 'last');
      cy.get('#menu a').eq(2).should('have.attr', 'href', 'https://incommon.org/help/').and('have.class', 'last');
    });

    it('Displays copyright text', () => {
      cy.contains('© Copyright 2020, InCommon, LLC').should('be.visible');
    });
  

    it('Displays Seamless Action button on page', () => {
      cy.fixture('testData').then((testData) => {
        const { entityID, returnUrl, error } = testData;
    
        // Visit the page with mock data as query parameters
        cy.visit(`http://localhost:3000/?entityID=${entityID}&return=${returnUrl}`);
        cy.get('a.sa-button') 
        .should('be.visible') 
        .and('have.attr', 'href') 
        .then((hrefAttribute) => {
          // Extract entityID and returnUrl from the href attribute
          const hrefParams = new URLSearchParams(hrefAttribute);
          const extractedEntityID = hrefParams.get('entityID');
          const extractedReturnUrl = hrefParams.get('returnUrl');

          // Assert that entityID and returnUrl are present and not empty
          // expect(extractedEntityID).to.be.a('string').and.not.empty;
          // expect(extractedReturnUrl).to.be.a('string').and.not.empty;

    
          cy.get('.sa-button-logo').should('be.visible');
          cy.get('.sa-button-text').should('be.visible').and('contain.text', 'Access through your institution');
        });
      });
    })

    it('Hides SA button and displays error message and wiki link if entityID or returnUrl are missing', () => {
      cy.get('.sa-button').should('not.exist');
      cy.get('h1').should('be.visible').and('contain.text', 'Both Entity ID and return URL are required.');
      cy.get('p a')
        .should('be.visible')
        .and('have.attr', 'href', 'https://incommon.org/help/');
      cy.get('p a').should('contain.text', 'Click Here');
    })
  })

  describe('SA button functionality', () => {

    it('Takes in entityID and returnUrl from URL and feeds to SA button', () => {
      cy.fixture('testData').then((testData) => {
        const { entityID, returnUrl, error } = testData;
    
        // Visit the page with mock data as query parameters
        cy.visit(`http://localhost:3000/?entityID=${entityID}&return=${returnUrl}`);
    
        // Check if the SA button is visible
        cy.get('.sa-button').should('be.visible');
    
        // Check if the Seamless Access button's href attribute contains the correct entityID and returnUrl
        cy.get('.sa-button').should(
          'have.attr',
          'href',
          `https://service.seamlessaccess.org/ds/?entityID=${entityID}&return=${returnUrl}`
        );
      });
    })
  })
})