import { posts as reducer } from '../../../pages/homepage/reducers/posts.js';

import { objectsFromArray } from '../../../utils.js';

import * as actions from '../../../pages/homepage/actions/posts.js';
import * as selectedActions from '../../../pages/homepage/actions/selected.js';
import * as constants from '../../../pages/homepage/constants.js';

const defaultState = { items: [], isFetching: false };

describe('post actions', () => {
  it('should return state after requesting posts', () => {
    const action = { type: actions.REQUEST_POSTS };

    const expectedState = { ...defaultState, isFetching: true, isUpdating: false };

    expect(reducer(undefined, action)).toEqual(expectedState);
  });

  it('should return state after receiving a post', () => {
    const post = {
      id: 2067,
      remoteIdentifier: 'https://arstechnica.com/?p=1648607',
      title:
        'This amazing glitch puts Star Fox 64 ships in an unmodified Zelda cartridge',
      body:
        '"Stale-reference manipulation," 300-character file names, and a clash between worlds.',
      author: 'Kyle Orland',
      publicationDate: '2020-01-24T19:50:12Z',
      url: 'https://arstechnica.com/?p=1648607',
      rule: 4,
      read: false,
      saved: false,
    };

    const action = {
      type: actions.RECEIVE_POST,
      post,
    };

    const expectedState = {
      ...defaultState,
      isFetching: false,
      isUpdating: false,
      items: [post],
    };

    expect(reducer(undefined, action)).toEqual(expectedState);
  });

  it('should return state after receiving posts', () => {
    const posts = [
      {
        id: 2067,
        remoteIdentifier: 'https://arstechnica.com/?p=1648607',
        title:
          'This amazing glitch puts Star Fox 64 ships in an unmodified Zelda cartridge',
        body:
          '"Stale-reference manipulation," 300-character file names, and a clash between worlds.',
        author: 'Kyle Orland',
        publicationDate: '2020-01-24T19:50:12Z',
        url: 'https://arstechnica.com/?p=1648607',
        rule: 4,
        read: false,
        saved: false,
      },
      {
        id: 2141,
        remoteIdentifier: 'https://arstechnica.com/?p=1648757',
        title: 'The most complete brain map ever is here: A fly’s “connectome”',
        body:
          'It took 12 years and at least $40 million to chart a region about 250µm across.',
        author: 'WIRED',
        publicationDate: '2020-01-25T11:06:46Z',
        url: 'https://arstechnica.com/?p=1648757',
        rule: 4,
        read: false,
        saved: false,
      },
    ];

    const action = {
      type: actions.RECEIVE_POSTS,
      next: 'https://durp.com/api/rules/4/posts/?page=2',
      posts,
    };

    const expectedState = {
      ...defaultState,
      isFetching: false,
      isUpdating: false,
      items: posts,
    };

    expect(reducer(undefined, action)).toEqual(expectedState);
  });

  it('should return state after marking a rule read', () => {
    const posts = {
      2067: {
        id: 2067,
        remoteIdentifier: 'https://arstechnica.com/?p=1648607',
        title:
          'This amazing glitch puts Star Fox 64 ships in an unmodified Zelda cartridge',
        body:
          '"Stale-reference manipulation," 300-character file names, and a clash between worlds.',
        author: 'Kyle Orland',
        publicationDate: '2020-01-24T19:50:12Z',
        url: 'https://arstechnica.com/?p=1648607',
        rule: {
          id: 5,
          name: 'Ars Technica',
          url: 'http://feeds.arstechnica.com/arstechnica/index?fmt=xml',
          favicon: 'https://cdn.arstechnica.net/favicon.ico',
          category: 9,
          unread: 544,
        },
        read: false,
      },
      2141: {
        id: 2141,
        remoteIdentifier: 'https://arstechnica.com/?p=1648757',
        title: 'The most complete brain map ever is here: A fly’s “connectome”',
        body:
          'It took 12 years and at least $40 million to chart a region about 250µm across.',
        author: 'WIRED',
        publicationDate: '2020-01-25T11:06:46Z',
        url: 'https://arstechnica.com/?p=1648757',
        rule: {
          id: 5,
          name: 'Ars Technica',
          url: 'http://feeds.arstechnica.com/arstechnica/index?fmt=xml',
          favicon: 'https://cdn.arstechnica.net/favicon.ico',
          category: 9,
          unread: 544,
        },
        read: false,
      },
      4637: {
        id: 4637,
        remoteIdentifier: 'https://www.bbc.co.uk/news/world-asia-china-51299195',
        title: "Coronavirus: Whole world 'must take action', warns WHO",
        body:
          'The World Health Organization will hold a further emergency meeting on the coronavirus on Thursday.',
        author: null,
        publicationDate: '2020-01-29T19:08:25Z',
        url: 'https://www.bbc.co.uk/news/world-asia-china-51299195',
        rule: {
          id: 4,
        },
        read: false,
        saved: false,
      },
      4638: {
        id: 4638,
        remoteIdentifier: 'https://www.bbc.co.uk/news/world-europe-51294305',
        title: "Coronavirus: French Asians hit back at racism with 'I'm not a virus'",
        body:
          'The coronavirus outbreak in Wuhan prompts French Asians to complain of a backlash against them.',
        author: null,
        publicationDate: '2020-01-29T18:27:56Z',
        url: 'https://www.bbc.co.uk/news/world-europe-51294305',
        rule: {
          id: 4,
        },
        read: false,
        saved: false,
      },
    };

    const rule = {
      id: 5,
      name: 'Ars Technica',
      url: 'http://feeds.arstechnica.com/arstechnica/index?fmt=xml',
      favicon: 'https://cdn.arstechnica.net/favicon.ico',
      category: 9,
      unread: 544,
    };

    const action = {
      type: selectedActions.MARK_SECTION_READ,
      section: { ...rule, type: constants.RULE_TYPE },
    };

    const state = { ...defaultState, items: Object.values(posts) };

    const expectedState = {
      ...defaultState,
      isFetching: false,
      items: [
        { ...posts[2067], read: true },
        { ...posts[2141], read: true },
        { ...posts[4637] },
        { ...posts[4638] },
      ],
    };

    expect(reducer(state, action)).toEqual(expectedState);
  });

  it('should return state after marking a category read', () => {
    const posts = {
      2067: {
        id: 2067,
        remoteIdentifier: 'https://arstechnica.com/?p=1648607',
        title:
          'This amazing glitch puts Star Fox 64 ships in an unmodified Zelda cartridge',
        body:
          '"Stale-reference manipulation," 300-character file names, and a clash between worlds.',
        author: 'Kyle Orland',
        publicationDate: '2020-01-24T19:50:12Z',
        url: 'https://arstechnica.com/?p=1648607',
        rule: {
          id: 5,
          name: 'BBC',
          url: 'http://feeds.bbci.co.uk/news/world/rss.xml',
          favicon:
            'https://m.files.bbci.co.uk/modules/bbc-morph-news-waf-page-meta/2.5.2/apple-touch-icon-57x57-precomposed.png',
          category: 8,
          unread: 632,
        },
        read: false,
        saved: false,
      },
      2141: {
        id: 2141,
        remoteIdentifier: 'https://arstechnica.com/?p=1648757',
        title: 'The most complete brain map ever is here: A fly’s “connectome”',
        body:
          'It took 12 years and at least $40 million to chart a region about 250µm across.',
        author: 'WIRED',
        publicationDate: '2020-01-25T11:06:46Z',
        url: 'https://arstechnica.com/?p=1648757',
        rule: {
          id: 5,
          name: 'BBC',
          url: 'http://feeds.bbci.co.uk/news/world/rss.xml',
          favicon:
            'https://m.files.bbci.co.uk/modules/bbc-morph-news-waf-page-meta/2.5.2/apple-touch-icon-57x57-precomposed.png',
          category: 8,
          unread: 632,
        },
        read: false,
        saved: false,
      },
      4637: {
        id: 4637,
        remoteIdentifier: 'https://www.bbc.co.uk/news/world-asia-china-51299195',
        title: "Coronavirus: Whole world 'must take action', warns WHO",
        body:
          'The World Health Organization will hold a further emergency meeting on the coronavirus on Thursday.',
        author: null,
        publicationDate: '2020-01-29T19:08:25Z',
        url: 'https://www.bbc.co.uk/news/world-asia-china-51299195',
        rule: {
          id: 4,
          name: 'BBC',
          url: 'http://feeds.bbci.co.uk/news/world/rss.xml',
          favicon:
            'https://m.files.bbci.co.uk/modules/bbc-morph-news-waf-page-meta/2.5.2/apple-touch-icon-57x57-precomposed.png',
          category: 8,
          unread: 321,
        },
        read: false,
        saved: false,
      },
      4638: {
        id: 4638,
        remoteIdentifier: 'https://www.bbc.co.uk/news/world-europe-51294305',
        title: "Coronavirus: French Asians hit back at racism with 'I'm not a virus'",
        body:
          'The coronavirus outbreak in Wuhan prompts French Asians to complain of a backlash against them.',
        author: null,
        publicationDate: '2020-01-29T18:27:56Z',
        url: 'https://www.bbc.co.uk/news/world-europe-51294305',
        rule: {
          id: 4,
          name: 'BBC',
          url: 'http://feeds.bbci.co.uk/news/world/rss.xml',
          favicon:
            'https://m.files.bbci.co.uk/modules/bbc-morph-news-waf-page-meta/2.5.2/apple-touch-icon-57x57-precomposed.png',
          category: 8,
          unread: 321,
        },
        read: false,
        saved: false,
      },
      4589: {
        id: 4589,
        remoteIdentifier: 'https://tweakers.net/nieuws/162878',
        title: 'Analyse: Nintendo verdiende miljard dollar aan mobiele games',
        body:
          'Nintendo heeft tot nu toe een miljard dollar verdiend aan mobiele games, zo heeft SensorTower becijferd. Daarbij gaat het om inkomsten uit de App Store van Apple en Play Store van Google. De game die het meeste opbracht is Fire Emblem Heroes.<img alt="" src="http://feeds.feedburner.com/~r/tweakers/mixed/~4/Yn_we8WaUeA">',
        author: 'Arnoud Wokke',
        publicationDate: '2020-01-29T19:03:01Z',
        url:
          'https://tweakers.net/nieuws/162878/analyse-nintendo-verdiende-miljard-dollar-aan-mobiele-games.html',
        rule: {
          id: 7,
        },
        read: false,
        saved: false,
      },
      4594: {
        id: 4594,
        remoteIdentifier: 'https://tweakers.net/nieuws/162870',
        title: 'Samsung kondigt eerste tablet met 5g aan',
        body:
          'Samsung heef zijn eerste tablet met 5g aangekondigd. Het gaat om een variant op de al bestaande Galaxy Tab S6, maar dan voorzien van Qualcomm X50-modem. Er gingen al maanden geruchten over de release van de tablet.<img alt="" src="http://feeds.feedburner.com/~r/tweakers/mixed/~4/IfEYe00sm3U">',
        author: 'Arnoud Wokke',
        publicationDate: '2020-01-29T16:29:40Z',
        url:
          'https://tweakers.net/nieuws/162870/samsung-kondigt-eerste-tablet-met-5g-aan.html',
        rule: {
          id: 7,
        },
        read: false,
        saved: false,
      },
    };

    const rules = {
      4: {
        id: 4,
        name: 'BBC',
        url: 'http://feeds.bbci.co.uk/news/world/rss.xml',
        favicon:
          'https://m.files.bbci.co.uk/modules/bbc-morph-news-waf-page-meta/2.5.2/apple-touch-icon-57x57-precomposed.png',
        category: 8,
        unread: 321,
      },
      5: {
        id: 5,
        name: 'BBC',
        url: 'http://feeds.bbci.co.uk/news/world/rss.xml',
        favicon:
          'https://m.files.bbci.co.uk/modules/bbc-morph-news-waf-page-meta/2.5.2/apple-touch-icon-57x57-precomposed.png',
        category: 8,
        unread: 632,
      },
    };

    const category = {
      id: 8,
      name: 'News',
      unread: 953,
    };

    const action = {
      type: selectedActions.MARK_SECTION_READ,
      section: {
        ...category,
        type: constants.CATEGORY_TYPE,
        rules,
      },
    };

    const state = { ...defaultState, items: Object.values(posts) };

    const expectedState = {
      ...defaultState,
      isFetching: false,
      items: [
        { ...posts[2067], read: true },
        { ...posts[2141], read: true },
        { ...posts[4589] },
        { ...posts[4594] },
        { ...posts[4637], read: true },
        { ...posts[4638], read: true },
      ],
    };

    expect(reducer(state, action)).toEqual(expectedState);
  });
});
