
const operations = {
  MeQuery: {
    me: {
      id: 'VXNlck5vZGU6NQ==',
      onboardingVisited: true,
      permissions: [],
    },
  },
  ProjectsQuery: {
    projects: {
      edges: [{
        node: {
          id: 'UHJvamVjdE5vZGU6MzM=',
          title: 'Groot',
          appearance: 'red',
          'description': 'I am Groot',
          'slug': 'groot',
          'objectives': 'Be Groot\nBe awesome',
          'final': false,
          'student': {
            'firstName': 'Rahel',
            'lastName': 'Cueni',
            'id': 'VXNlck5vZGU6NQ==',
            'avatarUrl': '',
            '__typename': 'UserNode',
          },
          'entriesCount': 2,
          '__typename': 'ProjectNode',
        },
        '__typename': 'ProjectNodeEdge',
      }],
      '__typename': 'ProjectNodeConnection',
    },
  },
  ProjectQuery: {
    'project': {
      'id': 'UHJvamVjdE5vZGU6MzY=',
      'title': 'Groot',
      'appearance': 'yellow',
      'description': 'I am Groot',
      'slug': 'groot',
      'objectives': 'Be Groot\nBe awesome',
      'final': false,
      'student': {
        'firstName': 'Rahel',
        'lastName': 'Cueni',
        'id': 'VXNlck5vZGU6NQ==',
        'avatarUrl': '',
        '__typename': 'UserNode',
      },
      'entriesCount': 1,
      '__typename': 'ProjectNode',
      'entries': {
        'edges': [{
          'node': {
            'id': 'UHJvamVjdEVudHJ5Tm9kZTo2NQ==',
            'activity': 'Kill Thanos',
            'reflection': 'He sucks',
            'nextSteps': 'Go for the head',
            'documentUrl': '',
            '__typename': 'ProjectEntryNode',
            'created': '2020-01-20T15:20:31.262510+00:00',
          },
          '__typename': 'ProjectEntryNodeEdge',
        }],
        '__typename': 'ProjectEntryNodeConnection',
      },
    },
  },
  AddProjectEntry: variables => ({
    addProjectEntry: {
      projectEntry: Object.assign({}, variables.input.projectEntry, {
        created: '2020-01-20T15:26:58.722773+00:00',
      }),
      errors: null,
      __typename: 'AddProjectEntryPayload',
    },
  }),
  UpdateProjectEntry: variables => ({
    updateProjectEntry: {
      projectEntry: variables.input.projectEntry,
      errors: null,
      __typename: 'UpdateProjectEntryPayload',
    },
  }),
  DeleteProjectEntry: {
    deleteProjectEntry: {
      success: true,
      __typename: 'DeleteProjectEntryPayload',
    },
  },
};

describe('Project Entry', () => {
  beforeEach(() => {
    cy.viewport('macbook-15');
    cy.fakeLogin('rahel.cueni', 'test');
    cy.server();

    cy.task('getSchema').then(schema => {
      cy.mockGraphql({
        schema,
        operations,
      });
    });
  });

  it('should create a new project entry', () => {
    cy.visit('/portfolio');
    cy.get('[data-cy=project-link]:first-of-type').click();
    cy.get('[data-cy=add-project-entry]:first-of-type').click();
    cy.get('[data-cy=activity-input]').within(() => {
      cy.get('[data-cy=text-form-input]').type('Join the Guardians');
    });
    cy.get('[data-cy=reflection-input]').within(() => {
      cy.get('[data-cy=text-form-input]').type('They are cool!');
    });
    cy.get('[data-cy=next-steps-input]').within(() => {
      cy.get('[data-cy=text-form-input]').type('Stay with Rocket\nMeet Quill');
    });
    cy.get('[data-cy=modal-save-button]').click();

    cy.get('.project-entry:last-of-type').within(() => {
      cy.get('.project-entry__paragraph:first-of-type').contains('Join the Guardians');
    });
  });

  it('should edit first entry', () => {
    cy.visit('/portfolio/groot');
    cy.get('.project-entry__paragraph:first-of-type').contains('Kill Thanos');
    cy.get('.project-entry:first-of-type').within(() => {
      cy.get('[data-cy=project-entry-more]').click();
      cy.get('[data-cy=edit-project-entry]').click();
    });
    cy.get('[data-cy=activity-input]').within(() => {
      cy.get('[data-cy=text-form-input]').clear().type('Defeat Thanos');
    });
    cy.get('[data-cy=modal-save-button]').click();
    cy.get('.project-entry__paragraph:first-of-type').contains('Defeat Thanos');
  });

  it('should delete the last entry', () => {
    cy.visit('/portfolio/groot');

    cy.get('.project-entry').should('have.length', 1);

    cy.get('.project-entry:last-of-type').within(() => {
      cy.get('[data-cy=project-entry-more]').click();
      cy.get('[data-cy=delete-project-entry]').click();
    });

    cy.get('.project-entry').should('have.length', 0);
  });
});
