summaryrefslogtreecommitdiffstats
path: root/backend/__tests__/item.test.js
blob: 7214a7ba575db3ca2109ab97258d9f835c71ef21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const { request } = require('graphql-request');
const createServer = require('../src/createServer');

let server;
const port = 2342;
const endpoint = `http://localhost:${port}`;

const wait = amount => new Promise(resolve => setTimeout(resolve, amount));

beforeAll(async () => {
  server = await createServer().start({ port });
});

afterAll(async () => {
  server.close();
});

describe('Item C.R.U.D. Operations', () => {
  let id;

  it('Creating an item', async () => {
    const query = `
    mutation createItem {
      createItem(title: "wes", description:"Bos", price: 500) {
        title
        description
        price
        id
      }
    }
  `;

    const { createItem } = await request(endpoint, query);

    expect(createItem.title).toEqual('wes');
    expect(createItem.description).toEqual('Bos');
    expect(createItem).toHaveProperty('id');
    expect(createItem.price).toEqual(500);
    id = createItem.id;
  });

  it('Get an array of items', async () => {
    const query = `
    query getAllItems {
      items {
        title
        id
        description
        price
      }
    }
  `;

    const { items } = await request(endpoint, query);
    expect(items.length).toBeGreaterThan(0);
  });

  it('Query a specific item', async () => {
    const query = `
    query findAnItem {
      items(where: {
        id: "${id}"
      }) {
        title
        id
        description
        price
      }
    }
  `;

    const { items } = await request(endpoint, query);
    const item = items[0];
    expect(item.title).toEqual('wes');
    expect(item.description).toEqual('Bos');
    expect(item.price).toEqual(500);
  });

  it('updates an item', async () => {
    const query = `
  mutation updateItem {
    updateItem(id: "${id}", title: "Updated Title") {
      id
      title
    }
  }
  `;
    const { updateItem } = await request(endpoint, query);
    expect(updateItem.id).toBe(id);
    expect(updateItem.title).toBe('Updated Title');
  });

  // Test Delete that item
  it('delete an item', async () => {
    const query = `
  mutation remove {
    deleteItem(id: "${id}") {
      id
    }
  }
  `;
    const res = await request(endpoint, query);
    expect(res.deleteItem.id).toEqual(id);
  });
});