const { registerAndLogin } = require('../../../../test/helpers/auth');
const createModelsUtils = require('../../../../test/helpers/models');
const { createAuthRequest } = require('../../../../test/helpers/request');

let modelsUtils;
let rq;

describe('Test type date', () => {
  beforeAll(async () => {
    const token = await registerAndLogin();
    rq = createAuthRequest(token);

    modelsUtils = createModelsUtils({ rq });

    await modelsUtils.createModelWithType('withdate', 'datetime');
  }, 60000);

  afterAll(async () => {
    await modelsUtils.deleteModel('withdate');
  }, 60000);

  test('Create entry with valid value JSON', async () => {
    const res = await rq.post(
      '/content-manager/explorer/application::withdate.withdate',
      {
        body: {
          field: '2019-08-08T10:10:57.000Z',
        },
      }
    );

    expect(res.statusCode).toBe(200);
    expect(res.body).toMatchObject({
      field: '2019-08-08T10:10:57.000Z',
    });
  });

  test('Create entry with valid value FormData', async () => {
    const now = new Date(2019, 0, 12);

    const res = await rq.post(
      '/content-manager/explorer/application::withdate.withdate',
      {
        formData: {
          data: JSON.stringify({ field: now }),
        },
      }
    );

    expect(res.statusCode).toBe(200);
    expect(res.body).toMatchObject({
      field: now.toISOString(),
    });
  });

  test('Create entry with timestamp value should be converted to ISO', async () => {
    const now = new Date(2016, 4, 8);

    const res = await rq.post(
      '/content-manager/explorer/application::withdate.withdate',
      {
        body: {
          field: now.getTime(),
        },
      }
    );

    expect(res.statusCode).toBe(200);
    expect(res.body).toMatchObject({
      field: now.toISOString(),
    });
  });

  test('Accepts string timestamp', async () => {
    const now = new Date(2000, 0, 1);

    const res = await rq.post(
      '/content-manager/explorer/application::withdate.withdate',
      {
        body: {
          field: `${now.getTime()}`,
        },
      }
    );

    expect(res.statusCode).toBe(200);
    expect(res.body).toMatchObject({
      field: now.toISOString(),
    });
  });

  test('Throws on invalid date format', async () => {
    const res = await rq.post(
      '/content-manager/explorer/application::withdate.withdate',
      {
        body: {
          field: 'azdazindoaizdnoainzd',
        },
      }
    );

    expect(res.statusCode).toBe(400);
  });

  test('Reading entry, returns correct value', async () => {
    const res = await rq.get(
      '/content-manager/explorer/application::withdate.withdate'
    );

    expect(res.statusCode).toBe(200);
    expect(Array.isArray(res.body)).toBe(true);
    res.body.forEach(entry => {
      expect(new Date(entry.field).toISOString()).toBe(entry.field);
    });
  });

  test('Updating entry sets the right value and format JSON', async () => {
    const now = new Date(2018, 7, 5);

    const res = await rq.post(
      '/content-manager/explorer/application::withdate.withdate',
      {
        body: {
          field: now.getTime(),
        },
      }
    );

    const newDate = new Date(2017, 10, 23);
    const updateRes = await rq.put(
      `/content-manager/explorer/application::withdate.withdate/${res.body.id}`,
      {
        body: {
          field: newDate,
        },
      }
    );

    expect(updateRes.statusCode).toBe(200);
    expect(updateRes.body).toMatchObject({
      id: res.body.id,
      field: newDate.toISOString(),
    });
  });
});

@@ -474,16 +474,20 @@
 withdate
+time
 ', 'date
@@ -490,24 +490,24 @@
 datetime');%0A
-
   %7D, 60000);
@@ -576,16 +576,20 @@
 withdate
+time
 ');%0A  %7D,
@@ -734,32 +734,36 @@
 cation::withdate
+time
 .withdate',%0A    
@@ -747,32 +747,36 @@
 atetime.withdate
+time
 ',%0A      %7B%0A     
@@ -1169,32 +1169,36 @@
 cation::withdate
+time
 .withdate',%0A    
@@ -1182,32 +1182,36 @@
 atetime.withdate
+time
 ',%0A      %7B%0A     
@@ -1623,32 +1623,36 @@
 cation::withdate
+time
 .withdate',%0A    
@@ -1636,32 +1636,36 @@
 atetime.withdate
+time
 ',%0A      %7B%0A     
@@ -2021,32 +2021,36 @@
 cation::withdate
+time
 .withdate',%0A    
@@ -2034,32 +2034,36 @@
 atetime.withdate
+time
 ',%0A      %7B%0A     
@@ -2390,32 +2390,36 @@
 cation::withdate
+time
 .withdate',%0A    
@@ -2403,32 +2403,36 @@
 atetime.withdate
+time
 ',%0A      %7B%0A     
@@ -2692,32 +2692,36 @@
 cation::withdate
+time
 .withdate'%0A    )
@@ -2713,16 +2713,20 @@
 withdate
+time
 '%0A    );
@@ -3121,32 +3121,36 @@
 cation::withdate
+time
 .withdate',%0A    
@@ -3142,16 +3142,20 @@
 withdate
+time
 ',%0A     
@@ -3366,16 +3366,20 @@
 withdate
+time
 .withdat
@@ -3379,16 +3379,20 @@
 withdate
+time
 /$%7Bres.b

Fix test conflicts