'use strict';

const {shallow, mount} = require('enzyme');
const Animate = require('../../lib/Animate');
const generateRandomSeries = require('../helpers/generateRandomSeries');
const later = require('../helpers/later');

const series1 = generateRandomSeries(3, 5, {type: 'object'});
const series2 = generateRandomSeries(3, 5, {type: 'object'});

describe('Animate', () => {

    it('should render g element and takes a series object', () => {
        const wrapper = shallow(<Animate series={series1} className='animate'>
            <span />
        </Animate>);
        const g = wrapper.find('g');
        expect(g.length).toEqual(1);
        expect(g.prop('className')).toEqual('animate');
        const expectedSeries = wrapper.find('span').prop('series');
        expect(expectedSeries).toEqual(series1);
    });

    pit('should interpolate props', () => {
        const wrapper = mount(<Animate
            series={series1}
            minX={0} maxX={2} minY={0} maxY={100}
            layerWidth={100} layerHeight={100}
            duration={100}>
            <span />
        </Animate>);
        const expectedSeries = wrapper.find('span').prop('series');
        expect(expectedSeries).toEqual(series1);
        jest.useRealTimers();
        wrapper.setProps({
            series: series2,
            minX: 1,
            maxX: 3,
            minY: -50,
            maxY: 50,
            layerWidth: 50,
            layerHeight: 150
        });

        return later(() => {
            const span = wrapper.find('span');
            const expectedSeries = span.prop('series');
            expect(expectedSeries).not.toEqual(series1);
            expect(expectedSeries).not.toEqual(series2);
            if (series1[0].data[0].y < series2[0].data[0].y) {
                expect(expectedSeries[0].data[0].y).toBeGreaterThan(series1[0].data[0].y);
                expect(expectedSeries[0].data[0].y).toBeLessThan(series2[0].data[0].y);
            } else {
                expect(expectedSeries[0].data[0].y).toBeLessThan(series1[0].data[0].y);
                expect(expectedSeries[0].data[0].y).toBeGreaterThan(series2[0].data[0].y);
            }
            expect(span.prop('minX')).toBeGreaterThan(0);
            expect(span.prop('maxX')).toBeGreaterThan(2);
            expect(span.prop('minY')).toBeLessThan(0);
            expect(span.prop('maxY')).toBeLessThan(100);
            expect(span.prop('layerWidth')).toBeLessThan(100);
            expect(span.prop('layerHeight')).toBeGreaterThan(100);

        }, 50).then(() => later(() => {
            const span = wrapper.find('span');
            const expectedSeries = span.prop('series');
            expect(expectedSeries).toEqual(series2);
            expect(span.prop('minX')).toEqual(1);
            expect(span.prop('maxX')).toEqual(3);
            expect(span.prop('minY')).toEqual(-50);
            expect(span.prop('maxY')).toEqual(50);
            expect(span.prop('layerWidth')).toEqual(50);
            expect(span.prop('layerHeight')).toEqual(150);

        }, 100));
    });

    pit('should log FPS metrics', () => {
        const wrapper = mount(<Animate
            series={series1}
            logFPS={true}
            duration={10}>
            <span />
        </Animate>);
        const expectedSeries = wrapper.find('span').prop('series');
        expect(expectedSeries).toEqual(series1);
        jest.useRealTimers();
        let consoleWarn = console.warn;
        console.warn = jasmine.createSpy('console.warn');
        wrapper.setProps({series: series2});

        return later(() => {
            const span = wrapper.find('span');
            const expectedSeries = span.prop('series');
            expect(expectedSeries).toEqual(series2);
            expect(console.warn).toHaveBeenCalled();
            const timer = wrapper.find(Animate).node._timer;
            spyOn(timer, 'stop');
            wrapper.unmount();
            expect(timer.stop).toHaveBeenCalledTimes(1);
            console.warn = consoleWarn;
        }, 50);
    });

});

@@ -1810,35 +1810,36 @@
 .data%5B0%5D.y).
-toBeGreater
+not.toBeLess
 Than(series1
@@ -1902,32 +1902,39 @@
 .data%5B0%5D.y).
+not.
 toBe
-Less
+Greater
 Than(series2
@@ -2022,24 +2022,31 @@
 a%5B0%5D.y).
-toBeLess
+not.toBeGreater
 Than(ser
@@ -2117,27 +2117,28 @@
 a%5B0%5D.y).
-toBeGreater
+not.toBeLess
 Than(ser
@@ -2205,35 +2205,36 @@
 op('minX')).
-toBeGreater
+not.toBeLess
 Than(0);%0A   
@@ -2264,35 +2264,36 @@
 op('maxX')).
-toBeGreater
+not.toBeLess
 Than(2);%0A   
@@ -2323,32 +2323,39 @@
 op('minY')).
+not.
 toBe
-Less
+Greater
 Than(0);%0A   
@@ -2385,32 +2385,39 @@
 op('maxY')).
-toBeLess
+not.toBeGreater
 Than(100);%0A 
@@ -2459,24 +2459,31 @@
 idth')).
-toBeLess
+not.toBeGreater
 Than(100
@@ -2530,27 +2530,28 @@
 ight')).
-toBeGreater
+not.toBeLess
 Than(100
@@ -3827,24 +3827,379 @@
 enCalled();%0A
+            console.warn = consoleWarn;%0A        %7D, 50);%0A    %7D);%0A%0A    pit('should stop timer on unmount', () =%3E %7B%0A        const wrapper = mount(%3CAnimate%0A            series=%7Bseries1%7D%0A            duration=%7B1%7D%3E%0A            %3Cspan /%3E%0A        %3C/Animate%3E);%0A%0A        jest.useRealTimers();%0A        wrapper.setProps(%7Bseries: series2%7D);%0A%0A        return later(() =%3E %7B%0A
             
@@ -4247,16 +4247,16 @@
 _timer;%0A
-
         
@@ -4373,48 +4373,8 @@
 1);%0A
-            console.warn = consoleWarn;%0A
     
@@ -4372,33 +4372,33 @@
 (1);%0A        %7D, 
-5
+3
 0);%0A    %7D);%0A%0A%7D);

update unit tests for Animate