/* global describe, it */

'use strict';

var cheerio = require('cheerio'),
    assert  = require('chai').assert,
    parser  = require('..');

describe('parser', function () {

  it('finds no data when none defined', function () {
    var $ = cheerio.load('<div>hello</div>');
    var result = parser.parse($);
    assert.isArray(result.items);
    assert.lengthOf(result.items, 0);
  });

  it('finds an empty scope', function () {
    var $ = cheerio.load('<div itemscope>hello</div>');
    var result = parser.parse($);
    assert.isArray(result.items);
    assert.lengthOf(result.items, 1);
    assert.isObject(result.items[0]);
    assert.isUndefined(result.items[0].type);
    assert.isObject(result.items[0].properties);
  });

  it('finds a scope with a type', function () {
    var $ = cheerio.load('<div itemscope itemtype="http://schema.org/Person">hello</div>');
    var result = parser.parse($);
    assert.isArray(result.items);
    assert.lengthOf(result.items, 1);
    assert.deepEqual(result.items[0].type, [ 'http://schema.org/Person' ]);
  });

  it('finds a scope with multiple types', function () {
    var $ = cheerio.load('<div itemscope itemtype=" http://schema.org/Person  http://schema.org/PostalAddress  ">hello</div>');
    var result = parser.parse($);
    assert.isArray(result.items);
    assert.lengthOf(result.items, 1);
    assert.deepEqual(result.items[0].type, [
      'http://schema.org/Person',
      'http://schema.org/PostalAddress'
    ]);
  });

  it('finds a scope with type defined twice', function () {
    var $ = cheerio.load('<div itemscope itemtype="http://schema.org/Person http://schema.org/Person">hello</div>');
    var result = parser.parse($);
    assert.isArray(result.items);
    assert.lengthOf(result.items, 1);
    assert.deepEqual(result.items[0].type, [
      'http://schema.org/Person'
    ]);
  });

  it('finds a scope within an element', function () {
    var $ = cheerio.load('<div><div itemscope itemtype="http://schema.org/Person">hello</div></div>');
    var result = parser.parse($);
    assert.isArray(result.items);
    assert.lengthOf(result.items, 1);
    assert.deepEqual(result.items[0].type, ['http://schema.org/Person']);
  });

  it('finds multiple scopes within an element', function () {
    var $ = cheerio.load(
      '<div>' +
      '<div itemscope itemtype="http://schema.org/Person">hello</div>' +
      '<div itemscope itemtype="http://schema.org/PostalAddress">hello</div>' +
      '</div>'
    );
    var result = parser.parse($);
    assert.isArray(result.items);
    assert.lengthOf(result.items, 2);
    assert.deepEqual(result.items[0].type, ['http://schema.org/Person']);
    assert.deepEqual(result.items[1].type, ['http://schema.org/PostalAddress']);
  });

  it('finds a scope with properties', function () {
    var $ = cheerio.load(
      '<div itemscope itemtype="http://schema.org/Person">' +
      '<div itemprop="name">Jan</div>' +
      '<div><div itemprop="age">29</div></div>' +
      '</div>'
    );
    var result = parser.parse($);
    assert.isArray(result.items);
    assert.lengthOf(result.items, 1);
    assert.deepEqual(result.items[0].properties, {
      name: ['Jan'],
      age: ['29']
    });
  });

  it('finds a scope with childscopes', function () {
    var $ = cheerio.load(
      '<div itemscope itemtype="http://schema.org/Person">' +
      '<div itemprop="address1" itemscope itemtype="http://schema.org/PostalAddress">' +
      '<div itemprop="street">street1</div>' +
      '</div>' +
      '<div itemprop="address2" itemscope itemtype="http://schema.org/PostalAddress">' +
      '<div itemprop="street">street2</div>' +
      '</div>' +
      '</div>'
    );
    var result = parser.parse($);
    assert.isArray(result.items);
    assert.lengthOf(result.items, 1);

    assert.deepEqual(result.items[0].properties.address1, [{
      type: ['http://schema.org/PostalAddress'],
      properties: { street: ['street1'] }
    }]);
    assert.deepEqual(result.items[0].properties.address2, [{
      type: ['http://schema.org/PostalAddress'],
      properties: { street: ['street2'] }
    }]);
  });

  it('collates properties', function () {
    var $ = cheerio.load(
      '<div itemscope itemtype="http://schema.org/Person">' +
      '<div itemprop="name">Jan</div>' +
      '<div itemprop="name">Potoms</div>' +
      '</div>'
    );
    var result = parser.parse($);
    assert.isArray(result.items);
    assert.lengthOf(result.items, 1);
    assert.deepEqual(result.items[0].properties, {
      name: ['Jan', 'Potoms']
    });
  });

});

@@ -405,21 +405,20 @@
 n empty 
-scope
+item
 ', funct
@@ -672,16 +672,60 @@
 .type);%0A
+    assert.isUndefined(result.items%5B0%5D.id);%0A
     asse
@@ -765,16 +765,81 @@
 rties);%0A
+    assert.lengthOf(Object.keys(result.items%5B0%5D.properties), 0);%0A
   %7D);%0A%0A 
@@ -846,30 +846,30 @@
  it('finds a
- scope
+n item
  with a type
@@ -1175,30 +1175,352 @@
  it('finds a
- scope
+n item with a global id', function () %7B%0A    var $ = cheerio.load('%3Cdiv itemscope itemid=%22urn:isbn:0-330-34032-8%22%3Ehello%3C/div%3E');%0A    var result = parser.parse($);%0A    assert.isArray(result.items);%0A    assert.lengthOf(result.items, 1);%0A    assert.deepEqual(result.items%5B0%5D.id, 'urn:isbn:0-330-34032-8');%0A  %7D);%0A%0A  it('finds an item
  with multip
@@ -1921,30 +1921,30 @@
  it('finds a
- scope
+n item
  with type d
@@ -2297,30 +2297,30 @@
  it('finds a
- scope
+n item
  within an e
@@ -2653,21 +2653,20 @@
 ultiple 
-scope
+item
 s within
@@ -3187,30 +3187,30 @@
  it('finds a
- scope
+n item
  with proper
@@ -3659,14 +3659,14 @@
 ds a
- scope
+n item
  wit
@@ -3672,21 +3672,20 @@
 th child
-scope
+item
 s', func

test for global ids