import {
  convertPageToPageEventIndexes,
  isRangeAvailableOnTheClient,
} from '../helpers/paginationHelpers';

describe('PaginationHelper tests', () => {
  describe('convertPageToPageEventIndex', () => {
    it('returns the proper page index', () => {
      let actualPage = 1;
      const actualEventsPerPage = 3;
      const actualFoundEventCount = 13;
      let expectedIndex = {
        start: 0,
        end: 2,
      };
      expect(
        convertPageToPageEventIndexes(
          actualPage,
          actualEventsPerPage,
          actualFoundEventCount
        )
      ).toEqual(expectedIndex);
      actualPage = 2;
      expectedIndex = {
        start: 3,
        end: 5,
      };
      expect(
        convertPageToPageEventIndexes(
          actualPage,
          actualEventsPerPage,
          actualFoundEventCount
        )
      ).toEqual(expectedIndex);
      actualPage = 3;
      expectedIndex = {
        start: 6,
        end: 8,
      };
      expect(
        convertPageToPageEventIndexes(
          actualPage,
          actualEventsPerPage,
          actualFoundEventCount
        )
      ).toEqual(expectedIndex);
      actualPage = 4;
      expectedIndex = {
        start: 9,
        end: 11,
      };
      expect(
        convertPageToPageEventIndexes(
          actualPage,
          actualEventsPerPage,
          actualFoundEventCount
        )
      ).toEqual(expectedIndex);
      actualPage = 5;
      expectedIndex = {
        start: 12,
        end: 12,
      };
      expect(
        convertPageToPageEventIndexes(
          actualPage,
          actualEventsPerPage,
          actualFoundEventCount
        )
      ).toEqual(expectedIndex);
    });

    it('returns the first page index when page is not provided', () => {
      const actualPage = null;
      const actualEventsPerPage = 3;
      const actualFoundEventCount = 13;
      const expectedIndex = {
        start: 0,
        end: actualEventsPerPage,
      };

      expect(
        convertPageToPageEventIndexes(
          actualPage,
          actualEventsPerPage,
          actualFoundEventCount
        )
      ).toEqual(expectedIndex);
    });

    it('returns the first page index when eventPerPage is not configured', () => {
      const actualPage = 2;
      const actualEventsPerPage = null;
      const actualFoundEventCount = 13;
      const expectedIndex = {
        start: 0,
        end: actualEventsPerPage,
      };

      expect(
        convertPageToPageEventIndexes(
          actualPage,
          actualEventsPerPage,
          actualFoundEventCount
        )
      ).toEqual(expectedIndex);
    });

    it('returns the first page index when the total number of event is not available', () => {
      const actualPage = 2;
      const actualEventsPerPage = 3;
      const actualFoundEventCount = null;
      const expectedIndex = {
        start: 0,
        end: actualEventsPerPage,
      };

      expect(
        convertPageToPageEventIndexes(
          actualPage,
          actualEventsPerPage,
          actualFoundEventCount
        )
      ).toEqual(expectedIndex);
    });

    it('returns the proper page index even if page is in string fromat', () => {
      const actualPage = '1';
      const actualEventsPerPage = 3;
      const actualFoundEventCount = 13;
      const expectedIndex = {
        start: 0,
        end: 2,
      };
      expect(
        convertPageToPageEventIndexes(
          actualPage,
          actualEventsPerPage,
          actualFoundEventCount
        )
      ).toEqual(expectedIndex);
    });
  });

  describe('isRangeAvailableOnTheClient', () => {
    describe('pageEventIndex is part of the downloadedEventIndex', () => {
      it('returns true; case 1', () => {
        const actualPageEventIndex = {
          start: 2,
          end: 5,
        };
        const actualDownloadedEventIndex = {
          start: 1,
          end: 6,
        };

        expect(
          isRangeAvailableOnTheClient(
            actualPageEventIndex,
            actualDownloadedEventIndex
          )
        ).toBe(true);
      });

      it('returns true; case 2', () => {
        const actualPageEventIndex = {
          start: 1,
          end: 2,
        };
        const actualDownloadedEventIndex = {
          start: 1,
          end: 6,
        };

        expect(
          isRangeAvailableOnTheClient(
            actualPageEventIndex,
            actualDownloadedEventIndex
          )
        ).toBe(true);
      });

      it('returns true; case 3', () => {
        const actualPageEventIndex = {
          start: 2,
          end: 6,
        };
        const actualDownloadedEventIndex = {
          start: 1,
          end: 6,
        };

        expect(
          isRangeAvailableOnTheClient(
            actualPageEventIndex,
            actualDownloadedEventIndex
          )
        ).toBe(true);
      });

      it('returns true; case 4', () => {
        const actualPageEventIndex = {
          start: 2,
          end: 3,
        };
        const actualDownloadedEventIndex = {
          start: 2,
          end: 3,
        };

        expect(
          isRangeAvailableOnTheClient(
            actualPageEventIndex,
            actualDownloadedEventIndex
          )
        ).toBe(true);
      });
    });

    describe('pageEventIndex is not part of the downloadedEventIndex', () => {
      it('returns false ; outside from the right side', () => {
        const actualPageEventIndex = {
          start: 3,
          end: 8,
        };
        const actualDownloadedEventIndex = {
          start: 1,
          end: 6,
        };

        expect(
          isRangeAvailableOnTheClient(
            actualPageEventIndex,
            actualDownloadedEventIndex
          )
        ).toBe(false);
      });

      it('returns false; outside from the left side', () => {
        const actualPageEventIndex = {
          start: 0,
          end: 4,
        };
        const actualDownloadedEventIndex = {
          start: 1,
          end: 6,
        };

        expect(
          isRangeAvailableOnTheClient(
            actualPageEventIndex,
            actualDownloadedEventIndex
          )
        ).toBe(false);
      });

      it('returns false; outside from both sides', () => {
        const actualPageEventIndex = {
          start: 5,
          end: 20,
        };
        const actualDownloadedEventIndex = {
          start: 4,
          end: 10,
        };

        expect(
          isRangeAvailableOnTheClient(
            actualPageEventIndex,
            actualDownloadedEventIndex
          )
        ).toBe(false);
      });
    });
  });
});
