import expect from 'expect';
import { stringify, parse } from '../src/index';

describe('deserializable', () => {
  it('should handle objects, arrays, etc. as usual', () => {
    const test = [{ a: 1, b: [2], c: true, d: "e" }];
    const stringified = stringify(test);
    const parsed = parse(stringified);

    expect(stringified).toBe(
      `[{"a":1,"b":[2],"c":true,"d":"e"}]`
    );
    expect(parsed instanceof Array).toBe(true);
  });

  it('should handle ES6 Map instances', () => {
    const test = new Map([['a', 1], ['b', 2]]);
    const stringified = stringify(test);
    const parsed = parse(stringified);

    expect(stringified).toBe(
      `{"__type__":"Map","__value__":[["a",1],["b",2]]}`
    );
    expect(parsed instanceof Map).toBe(true);
  });

  it('should handle ES6 Set instances', () => {
    const test = new Set(['a', 'a', 'b', 'b']);
    const stringified = stringify(test);
    const parsed = parse(stringified);

    expect(stringified).toBe(
      `{"__type__":"Set","__value__":["a","b"]}`
    );
    expect(parsed instanceof Set).toBe(true);
  });

  it('should handle RegExp instances', () => {
    const test = /[^A-z0-9]/gi;
    const stringified = stringify(test);
    const parsed = parse(stringified);

    expect(stringified).toBe(
      `{"__type__":"RegExp","__value__":"/[^A-z0-9]/gi"}`
    );
    expect(parsed instanceof RegExp).toBe(true);
    expect(parsed.toString()).toBe(test.toString());
  });

  it('should handle Date instances', done => {
    const time = Date.now();
    const test = new Date(time);
    const stringified = stringify(test);
    const parsed = parse(stringified);

    setTimeout(() => {
      expect(stringified).toBe(
        `{"__type__":"Date","__value__":"${new Date(time).toJSON()}"}`
      );
      expect(parsed instanceof Date).toBe(true);
      expect(parsed.getTime()).toBe(time);
      done();
    }, 100);
  });
});
