import assert from 'assert';
import React from 'react';
import { shallow } from 'enzyme';
import sinon from 'sinon';

import { List } from './List';

describe('<List />', () => {
    const defaultProps = {
        resource: 'post',
        hasCreate: false,
        hasEdit: false,
        location: {},
        params: {},
        query: {},
        filterValues: {},
        total: 100,
        isLoading: false,
        crudGetList: () => {},
        push: () => {},
        translate: () => {},
    };

    describe('Content Values', () => {
      it('check the no results text is displayed correctly', () => {
        const translateSpy = sinon.spy();
        const wrapper = shallow(
            <List
                {...defaultProps}
                translate={translateSpy}
                children={[]}
                total={0}
            >
                <div />
            </List>
        );

        assert.equal(translateSpy.calledWith('aor.navigation.no_results'), true);

        const translateSpy2 = sinon.spy();
        const wrapper2 = shallow(
            <List
                {...defaultProps}
                translate={translateSpy2}
                children={[ 'not_empty' ]}
                total={1}
            >
                <div />
            </List>
        );

        assert.equal(translateSpy2.calledWith('aor.navigation.no_results'), false);
      });
    });

    describe('Filters', () => {
        let clock;
        beforeEach(() => {
            clock = sinon.useFakeTimers();
        });

        it('should call `changeListParams` prop function for each filter change', () => {
            const changeListParams = sinon.spy();
            const wrapper = shallow(
                <List
                    {...defaultProps}
                    changeListParams={changeListParams}
                    filterValues={{}}
                >
                    <div />
                </List>
            );

            wrapper.setProps({ filterValues: { q: 'hello' } });
            clock.tick(1000); // wait for debounce
            wrapper.setProps({ filterValues: {} });
            clock.tick(1000); // wait for debounce

            assert.deepEqual(changeListParams.args, [
                ['post', { page: 1, perPage: 10, sort: 'id', order: 'DESC', filter: { q: 'hello' } }],
                ['post', { page: 1, perPage: 10, sort: 'id', order: 'DESC', filter: {} }],
            ]);
        });

        it('should call `changeFormValue` prop function when a filter is removed (hidden)', () => {
            const changeListParams = sinon.spy();
            const changeFormValue = sinon.spy();
            const wrapper = shallow(
                <List
                    {...defaultProps}
                    changeListParams={changeListParams}
                    changeFormValue={changeFormValue}
                >
                    <div />
                </List>
            );

            wrapper.instance().hideFilter('q');

            assert.deepEqual(changeFormValue.args, [
                ['filterForm', 'q', ''],
            ]);
        });

        afterEach(() => {
            if (clock) {
                clock.restore();
            }
        });
    });
});

@@ -508,153 +508,78 @@
     
-describe('Content Values', () =%3E %7B%0A      it('check the no results text is displayed correctly', () =%3E %7B%0A        const translateSpy = sinon.spy();
+it('should display a no results text when there is no result', () =%3E %7B
 %0A   
@@ -687,28 +687,22 @@
 nslate=%7B
-translateSpy
+x =%3E x
 %7D%0A      
@@ -767,111 +767,262 @@
     
-%3E%0A                %3Cdiv /%3E%0A            %3C/List%3E%0A        );%0A%0A        assert.equal(translateSpy.calledWith(
+    changeFormValue=%7B() =%3E true%7D%0A                changeListParams=%7B() =%3E true%7D%0A            %3E%0A                %3Cdiv /%3E%0A            %3C/List%3E%0A        );%0A        const textElement = wrapper.find('CardText').children();%0A        assert.equal(textElement.text(), 
 'aor
@@ -1049,22 +1049,23 @@
 ts')
-, true
+;%0A    %7D
 );%0A%0A    
     
@@ -1064,46 +1064,81 @@
     
-    const translateSpy2 = sinon.spy();
+it('should not display a no results text when there are results', () =%3E %7B
 %0A   
@@ -1155,17 +1155,16 @@
  wrapper
-2
  = shall
@@ -1246,29 +1246,22 @@
 nslate=%7B
-translateSpy2
+x =%3E x
 %7D%0A      
@@ -1323,16 +1323,107 @@
 tal=%7B1%7D%0A
+                changeFormValue=%7B() =%3E true%7D%0A                changeListParams=%7B() =%3E true%7D%0A
         
@@ -1483,25 +1483,24 @@
      );%0A
-%0A
         
 assert.e
@@ -1495,91 +1495,96 @@
     
-assert.equal(translateSpy2.calledWith('aor.navigation.no_results'), false);%0A      %7D
+const textElement = wrapper.find('CardText');%0A        assert.equal(textElement.length, 0
 );%0A 

Refactor tests