import {getMinimalMe} from '../../../support/helpers';

const getOperations = (readOnly = false, classReadOnly = false) => {
  const MeQuery = getMinimalMe({readOnly, classReadOnly});
  const me = MeQuery.me;

  return {
  MeQuery,
  MySchoolClassQuery: {
    me: {
      ...me,
      id: 'meId',
      selectedClass: {
        ...me.selectedClass,
        members: [
          {
            id: 'meId',
            firstName: 'Helge',
            lastName: 'Schneider',
            isTeacher: true,
            isMe: true,
            active: true
          },
          {
            id: 'notMeId',
            firstName: 'Otto',
            lastName: 'Waalkes',
            isTeacher: false,
            isMe: false,
            active: true
          },
          {
            id: 'alsoNotMeId',
            firstName: 'Kaya',
            lastName: 'Yanar',
            isTeacher: false,
            isMe: false,
            active: false
          }
        ]
      }
    },
  },
  AddRemoveMember: {
    addRemoveMember: {
      success: true
    }
  }
};
};

describe('Leave School Class', () => {
  beforeEach(() => {
    cy.setup();
  });

  it('can leave class', () => {
    cy.mockGraphqlOps({
      operations: getOperations(),
    });

    cy.visit('/me/class');

    cy.getByDataCy('remove-from-class').should('exist');
    cy.getByDataCy('add-to-class').should('exist');
    cy.getByDataCy('leave-group').click();
    cy.getByDataCy('modal-save-button').click();
    cy.getByDataCy('read-only-banner').should('exist');
  });

  it('can not leave class when license invalid', () => {
    cy.mockGraphqlOps({
      operations: getOperations(true),
    });

    cy.visit('/me/class');

    cy.getByDataCy('remove-from-class').should('not.exist');
    cy.getByDataCy('add-to-class').should('not.exist');
    cy.getByDataCy('leave-group').should('not.exist');
  });

  it('can not leave class when class inactive', () => {
    cy.mockGraphqlOps({
      operations: getOperations(false, true),
    });

    cy.visit('/me/class');

    cy.getByDataCy('remove-from-class').should('not.exist');
    cy.getByDataCy('add-to-class').should('not.exist');
    cy.getByDataCy('leave-group').should('not.exist');
  });
});
