/*
 * Copyright (c) 2015 by Greg Reimer <gregreimer@gmail.com>
 * MIT License. See mit-license.txt for more info.
 */

/* eslint-env mocha */

import assert from 'assert';
import munge from '../src/munge';

describe('munge', () => {

  it('should mutate top-level items of an array', () => {
    const obj = ['a','b','c'];
    munge(obj, [{ select: ['$index'], edit: val => val.toUpperCase() }]);
    assert.deepEqual(obj, ['A','B','C']);
  });

  it('should mutate top-level props of an object', () => {
    const obj = {a:'a',b:'b'};
    munge(obj, [{ select: ['$key'], edit: val => val.toUpperCase() }]);
    assert.deepEqual(obj, {a:'A',b:'B'});
  });

  it('should selectively mutate top-level props of an object', () => {
    const obj = {a:'a',b:'b'};
    munge(obj, [{ select: ['b'], edit: val => val.toUpperCase() }]);
    assert.deepEqual(obj, {a:'a',b:'B'});
  });

  it('should mutate second-level props of an object', () => {
    const obj = {a:'a',b:['b','c']};
    munge(obj, [{ select: ['b','$index'], edit: val => val.toUpperCase() }]);
    assert.deepEqual(obj, {a:'a',b:['B','C']});
  });

  it('should selectively mutate second-level props of an object', () => {
    const obj = {a:'a',b:['b','c']};
    munge(obj, [{ select: ['b',0], edit: val => val.toUpperCase() }]);
    assert.deepEqual(obj, {a:'a',b:['B','c']});
  });

  it('should delete props from an object', () => {
    const obj = {a:'a',b:{c:2}};
    munge(obj, [{ select: ['b','c'], edit: () => undefined }]);
    assert.deepEqual(obj, {a:'a',b:{}});
  });

  it('should not delete props from an array', () => {
    const obj = {a:'a',b:[1,2]};
    munge(obj, [{ select: ['b','$index'], edit: () => undefined }]);
    assert.deepEqual(obj, {a:'a',b:[undefined,undefined]});
  });

  it('should munge its own munges', () => {
    const obj = { a: 'a', b: null };
    munge(obj, [{
      select: ['b'],
      edit: () => ({ c: true })
    }, {
      select: ['b','c'],
      edit: val => !val
    }]);
    assert.deepEqual(obj, { a: 'a', b: { c: false } });
  });

  it('should mutate the root item', () => {
    const obj = ['a','b','c'];
    munge(obj, [{ select: [], edit: root => root.push('d') }]);
    assert.deepEqual(obj, ['a','b','c','d']);
  });

  it('should pass parent', () => {
    const obj = ['a','b','c'];
    function edit(val, parent) {
      assert.deepEqual(parent, ['a','b','c']);
      return val.toUpperCase();
    }
    munge(obj, [{ select: [0], edit }]);
    assert.deepEqual(obj, ['A','b','c']);
  });
});

@@ -2504,13 +2504,296 @@
 );%0A  %7D);
+%0A%0A  it('should pass parent prop', () =%3E %7B%0A    const obj = %5B'a','b','c'%5D;%0A    function edit(val, parent, prop) %7B%0A      assert.strictEqual(parent%5Bprop%5D, val);%0A      return val.toUpperCase();%0A    %7D%0A    munge(obj, %5B%7B select: %5B0%5D, edit %7D%5D);%0A    assert.deepEqual(obj, %5B'A','b','c'%5D);%0A  %7D);
 %0A%7D);%0A

test for parent prop