import {getNextStep, getLatestUncompletedStep, getlastCompletedStep, checkStepCompletedBefore} from '../../../server/plugins/register-form/steps/common';

describe('Get next step', () => {

  it('should give next step unconditionally if no checkApplies function', () => {
    const nextSteps = [
      {key: 'a'},
      {key: 'b'}
    ];
    expect(getNextStep(nextSteps, {})).toEqual('a');
  });

  it('should call checkApplies if defined', () => {
    const mockCheckApplies = jest.fn();
    mockCheckApplies.mockReturnValueOnce(false);

    const nextSteps = [
      {key: 'a', checkApplies: mockCheckApplies},
      {key: 'b'}
    ];
    const cookieData = {};
    getNextStep(nextSteps, cookieData);
    // The function was called exactly once
    expect(mockCheckApplies.mock.calls.length).toBe(1);
    // The first arg of the first call to the function was '{}'
    expect(mockCheckApplies.mock.calls[0][0]).toBe(cookieData);
  });

  it('should call checkApplies and return a if it passes', () => {
    const mockCheckApplies = jest.fn();
    mockCheckApplies.mockReturnValueOnce(true);

    const nextSteps = [
      {key: 'a', checkApplies: mockCheckApplies},
      {key: 'b'}
    ];
    expect(getNextStep(nextSteps, {})).toEqual('a');
  });

  it('should call checkApplies and return b if it fails', () => {
    const mockCheckApplies = jest.fn();
    mockCheckApplies.mockReturnValueOnce(false);

    const nextSteps = [
      {key: 'a', checkApplies: mockCheckApplies},
      {key: 'b'}
    ];
    expect(getNextStep(nextSteps, {})).toEqual('b');
  });

  it('should call getLatestUncompletedStep and return right step', () => {
    const cookieData = {
      "name": {
        "a": "b"
      }
    };

    expect(getLatestUncompletedStep(cookieData).key).toEqual('dateOfBirth');
  });

  it('should call getlastCompletedStep and return right step', () => {
    const cookieData = {
      "name": {
        "a": "b"
      },
      "dateOfBirth": {
        "c": "d"
      }
    };

    expect(getlastCompletedStep(cookieData).key).toEqual('dateOfBirth');
  });

  it('should call checkStepCompletedBefore and return true', () => {
    const cookieData = {
      "name": {
        "a": "b"
      },
      "dateOfBirth": {
        "c": "d"
      }
    };
    const latestUncompletedStep = getLatestUncompletedStep(cookieData);
    const requestedStepKey = "name";
    expect(checkStepCompletedBefore(requestedStepKey, latestUncompletedStep)).toEqual(true)
  });

});

@@ -1659,38 +1659,38 @@
 eData = %7B%0A      
-%22
+'
 name
-%22
+'
 : %7B%0A        %22a%22:
@@ -1681,32 +1681,32 @@
 : %7B%0A        
-%22a%22: %22b%22
+'a': 'b'
 %0A      %7D%0A   
@@ -1888,38 +1888,38 @@
 eData = %7B%0A      
-%22
+'
 name
-%22
+'
 : %7B%0A        %22a%22:
@@ -1910,32 +1910,32 @@
 : %7B%0A        
-%22a%22: %22b%22
+'a': 'b'
 %0A      %7D,%0A  
@@ -1930,33 +1930,33 @@
 %0A      %7D,%0A      
-%22
+'
 dateOfBirth%22: %7B%0A
@@ -1942,33 +1942,33 @@
     'dateOfBirth
-%22
+'
 : %7B%0A        %22c%22:
@@ -1959,32 +1959,32 @@
 : %7B%0A        
-%22c%22: %22d%22
+'c': 'd'
 %0A      %7D%0A   
@@ -2168,22 +2168,22 @@
 %7B%0A      
-%22
+'
 name
-%22
+'
 : %7B%0A    
@@ -2190,16 +2190,16 @@
     
-%22a%22: %22b%22
+'a': 'b'
 %0A   
@@ -2210,17 +2210,17 @@
 ,%0A      
-%22
+'
 dateOfBi
@@ -2222,17 +2222,17 @@
 eOfBirth
-%22
+'
 : %7B%0A    
@@ -2239,16 +2239,16 @@
     
-%22c%22: %22d%22
+'c': 'd'
 %0A   
@@ -2364,14 +2364,14 @@
 y = 
-%22
+'
 name
-%22
+'
 ;%0A  
@@ -2459,16 +2459,17 @@
 al(true)
+;
 %0A  %7D);%0A%0A

clean lint warnings