Esempio n. 1
0
  test('fetch deletes host header from Request with object', async () => {
    fetchMock.mockResponse('')
    await shimFetch(new Request('https://www.google.com', {headers: {host: 'test.com'}}))

    const headers = fetchMock.mock.calls[0][0].headers || fetchMock.mock.calls[0][1].headers || {}
    expect(new Headers(headers).get('host')).toBeNull()
  })
    it('test REMOVE_COMMENT action', () => {

        fetchMock.mockResponse(JSON.stringify(responseAll))

        window.fetch = fetchMock;

        const expectedActions = [
            { type: CommentActions.REMOVE_COMMENT, comment }
        ];

        const store = mockStore({})

        return store.dispatch(CommentActions.deleteComment(comment)).then(() => {
            expect(store.getActions()).toEqual(expectedActions)
        });
    });
    it('test LOAD_COMMENTS action', () => {

        fetchMock.mockResponse(JSON.stringify(responseAll))

        window.fetch = fetchMock;

        const expectedActions = [
            { type: CommentActions.LOAD_COMMENTS, comments: responseAll }
        ];

        const store = mockStore({})

        return store.dispatch(CommentActions.fetchCommentsByPostId("postId")).then(() => {
            expect(store.getActions()).toEqual(expectedActions)
        });
    });
    it('test LOAD_POSTS action', () => {

        fetchMock.mockResponse(JSON.stringify(responseAll))

        window.fetch = fetchMock;

        const expectedActions = [
            { type: PostActions.LOAD_POSTS, posts: responseAll }
        ];

        const store = mockStore({})

        return store.dispatch(PostActions.fetchPosts()).then(() => {
            expect(store.getActions()).toEqual(expectedActions)
        });
    });
    it('test ADD_POST action', () => {

        fetchMock.mockResponse(JSON.stringify(responseReact))

        window.fetch = fetchMock;

        const post = responseReact[0];

        const expectedActions = [
            { type: PostActions.ADD_POST, post }
        ];

        const store = mockStore({})

        return store.dispatch(PostActions.addNewPost(post)).then(() => {
            expect(store.getActions()).toEqual(expectedActions)
        });
    });
    it('test LOAD_POSTS_BY_CATEGORY action', () => {

        fetchMock.mockResponse(JSON.stringify(responseReact))

        window.fetch = fetchMock;

        const category = "react";

        const expectedActions = [
            { type: PostActions.LOAD_POSTS_BY_CATEGORY, posts: responseReact, category }
        ];

        const store = mockStore({})

        return store.dispatch(PostActions.fetchPostsByCategory(category)).then(() => {
            expect(store.getActions()).toEqual(expectedActions)
        });
    });
    it('test DOWN_VOTE_POST action', () => {

        fetchMock.mockResponse(JSON.stringify(responseReact))

        window.fetch = fetchMock;

        const postId = responseReact[0].id;

        const expectedActions = [
            { type: PostActions.DOWN_VOTE_POST, postId }
        ];

        const store = mockStore({})

        return store.dispatch(PostActions.downVotingPost(postId)).then(() => {
            expect(store.getActions()).toEqual(expectedActions)
        });
    });
Esempio n. 8
0
 test('fetch freezes response headers', async () => {
   fetchMock.mockResponse('', {headers: new Headers()})
   const resp = await shimFetch('https://www.google.com')
   expect(() => { resp.headers.set('test', 'test') }).toThrowError()
 })