import module from '../../fixtures/module.minimal';
import {getMinimalMe} from '../../support/helpers';

describe('Snapshot', () => {
  const operations = isTeacher => ({
    operations: {
      MeQuery: getMinimalMe({isTeacher}),
      ModuleDetailsQuery: {
        module,
      },
      CreateSnapshot: {
        createSnapshot: {
          snapshot: {
            id: '',
            title: '',
            created: '',
            creator: '',
          },
          success: true,
        },
      },
      ModuleSnapshotsQuery: {
        module: {
          ...module,
          snapshots: [
            {
              id: 'snapshot-id',
              title: 'title',
              created: '2020-01-01',
              mine: true,
              shared: false,
              creator: 'me',
            },
          ],
        },
      },
      SnapshotDetail: {
        snapshot: {
          chapters: [],
          module: {}
        }
      },
      ApplySnapshot: {
        applySnapshot: {
          success: true
        }
      }
    },
  });

  beforeEach(() => {
    cy.setup();
  });

  it('Menu is visible for teacher', () => {
    cy.mockGraphqlOps(operations(true));
    cy.visit('module/miteinander-reden/');
    cy.getByDataCy('snapshot-menu').should('be.visible');
  });

  it('Menu is not visible for student', () => {
    cy.mockGraphqlOps(operations(false));
    cy.visit('module/miteinander-reden/');

    cy.getByDataCy('module-title').should('be.visible');
    cy.getByDataCy('snapshot-menu').should('not.exist');
  });

  it('Creates Snapshot', () => {
    cy.mockGraphqlOps(operations(true));
    cy.visit('module/miteinander-reden/');
    cy.getByDataCy('module-snapshots-button').click();
    cy.getByDataCy('create-snapshot-button').click();
    cy.getByDataCy('show-all-snapshots-button').click();
    cy.getByDataCy('snapshot-list').should('exist').within(() => {
      cy.get('.snapshots__snapshot').should('have.length', 1);
    });
  });

  it('Applies Snapshot', () => {
    cy.mockGraphqlOps(operations(true));
    cy.visit('module/miteinander-reden/snapshots');
    cy.getByDataCy('snapshot-link').click();
    cy.getByDataCy('apply-checkbox').click();
    cy.getByDataCy('apply-button').click();

    cy.getByDataCy('module-title').should('exist');
    cy.getByDataCy('snapshot-header').should('not.exist');
  });
});
