diff --git a/CHANGELOG.md b/CHANGELOG.md index 41dd9649..07fd2eee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +### 2.2.0 + +- **[Feature]** #682 Multiple word search +- **[Feature]** #683 Debounced keyup handler in search +- **[Website]** #684 Add more documentation for automagical search and sort elements + ### 2.1.0 - 2020-11-21 - **[Feature]** #634 Add item template function @@ -12,8 +18,6 @@ ### 2.0.0 - 2020-11-21 - Winter cleanup 🧹 - **[Breaking]** Drop support for IE6-8 -- **[Website]** Update Jekyll to remove security warnings -- **[Website]** Fix all examples (sorry that they we're broken) - **[Misc]** Update dev dependencies to latest version: Webpack 3.12.0 -> 5.6.0, jest 23.3 -> 26.6.3, jquery 3.3.1 -> 3.5.1, Removed: jshint jshint-loader - **[Misc]** Replace uglify-js with terser - **[Misc]** Update Node for dev from 6.15 to 14.15.1 @@ -22,14 +26,12 @@ - **[Misc]** Rename History.md to CHANGELOG.md - **[Misc]** Use `babal-loader` with `@babel/preset-env` for supporting IE9-11 - **[Misc]** Add source-maps to `/dist` - -### 1.5.1 - - **[Misc]** Added WISHLIST.md for feature requests to allow cleanup of issue list. - **[Misc]** Update CircleCI from 1.0 to 2.0 +- **[Website]** Update Jekyll to remove security warnings +- **[Website]** Fix all examples (sorry that they we're broken) - **[Website]** Use https instead of http for listjs.com - **[Website]** Update Contribute guidelines [See commit →](https://github.com/javve/list.js/commit/6242496de2ac5c07903fb1590a5cb5129f0887a7) -- **[Website]** Update Jekyll & jQuery versions to remove security warnings. - **[Bugfix]** Use one event listener per pagination and select page via data attributes [See commit →](https://github.com/javve/list.js/commit/7610c59039f3b39f52175cd1a200e935664869e8) - **[Bugfix]** Don't break pagination if page=0 diff --git a/__test__/create.test.js b/__test__/create.test.js deleted file mode 100644 index bae4c927..00000000 --- a/__test__/create.test.js +++ /dev/null @@ -1,303 +0,0 @@ -const $ = require('jquery'), - List = require('../src/index') - -describe('Create', function () { - describe('With HTML items', function () { - var listEl = $( - '
\ - \ -
' - ) - - $(document.body).append(listEl) - - var list = new List('list', { valueNames: ['name'] }) - - it('should contain one item', function () { - expect(list.items.length).toEqual(1) - expect(listEl.find('li').length).toEqual(1) - }) - - it('should contain two items', function () { - list.add({ name: 'Jonas' }) - expect(list.items.length).toEqual(2) - expect(listEl.find('li').length).toEqual(2) - }) - - listEl.remove() - }) - - describe('With and element instead of id', function () { - var listEl = $( - '
\ - \ -
' - ) - - $(document.body).append(listEl) - var el = document.getElementById('list') - - var list = new List(el, { valueNames: ['name'] }) - - it('should contain one item', function () { - expect(list.items.length).toEqual(1) - expect(listEl.find('li').length).toEqual(1) - }) - - listEl.remove() - }) - - describe('Without items and with string template', function () { - var listEl = $('
\ - \ -
') - - $(document.body).append(listEl) - - var list = new List( - 'list', - { - valueNames: ['name'], - item: '
  • ', - }, - [{ name: 'Jonny' }] - ) - - it('should contain one item', function () { - expect(list.items.length).toEqual(1) - expect(listEl.find('li').length).toEqual(1) - }) - - it('should contain two items', function () { - list.add({ name: 'Jonas' }) - expect(list.items.length).toEqual(2) - expect(listEl.find('li').length).toEqual(2) - }) - - listEl.remove() - }) - - describe('Without items and with string template for table', function () { - var listEl = $('
    \ -
    \ -
    ') - - $(document.body).append(listEl) - - var list = new List( - 'list', - { - valueNames: ['name'], - item: '', - }, - [{ name: 'Jonny' }] - ) - - it('should contain one item', function () { - expect(list.items.length).toEqual(1) - expect(listEl.find('tr').length).toEqual(1) - }) - - it('should contain two items', function () { - list.add({ name: 'Jonas' }) - expect(list.items.length).toEqual(2) - expect(listEl.find('tr').length).toEqual(2) - }) - - listEl.remove() - }) - - describe('Without items and with template function', function () { - var listEl = $('
    \ - \ -
    ') - - $(document.body).append(listEl) - - var list = new List( - 'list', - { - valueNames: ['name'], - item: function (values) { - return `
  • ` - }, - }, - [{ name: 'Jonny' }] - ) - - it('should contain one item', function () { - expect(list.items.length).toEqual(1) - expect(listEl.find('li').length).toEqual(1) - }) - - it('should contain two items', function () { - list.add({ name: 'Jonas' }) - expect(list.items.length).toEqual(2) - expect(listEl.find('li').length).toEqual(2) - }) - - it('should get values from items', function () { - list.add({ name: 'Egon' }) - expect(listEl.find('li[data-template-fn-egon]').length).toEqual(1) - }) - - listEl.remove() - }) - - describe('without items and or template', function () { - it('should throw error on init', function () { - var listEl = $('
    \ - \ -
    ') - $(document.body).append(listEl) - - expect(function () { - var list = new List('list', { - valueNames: ['name'], - }) - }).toThrow() - listEl.remove() - }) - }) - - describe('Without items and with HTML template', function () { - var listEl = $('
    \ - \ -
    ') - - var templateEl = $('
  • ') - - $(document.body).append(listEl) - $(document.body).append(templateEl) - - var list = new List( - 'list', - { - valueNames: ['name'], - item: 'template-item', - }, - [{ name: 'Jonny' }] - ) - - it('should contain one item', function () { - expect(list.items.length).toEqual(1) - expect(listEl.find('li').length).toEqual(1) - }) - - it('should contain two items', function () { - list.add({ name: 'Jonas' }) - expect(list.items.length).toEqual(2) - expect(listEl.find('li').length).toEqual(2) - }) - - listEl.remove() - templateEl.remove() - }) - - describe('Asyn index with existing list', function () { - var listEl = $( - '
    \ - \ -
    ' - ) - - it('should contain one item', function (done) { - $(document.body).append(listEl) - var list = new List('list', { - valueNames: ['name'], - indexAsync: true, - parseComplete: function (list) { - expect(listEl.find('li').length).toEqual(162) - listEl.remove() - done() - }, - }) - }) - }) -}) diff --git a/__test__/add-get-remove.test.js b/__test__/integration/add-get-remove.test.js similarity index 100% rename from __test__/add-get-remove.test.js rename to __test__/integration/add-get-remove.test.js diff --git a/__test__/buttons.test.js b/__test__/integration/buttons.test.js similarity index 99% rename from __test__/buttons.test.js rename to __test__/integration/buttons.test.js index 2165579e..ca6103fa 100644 --- a/__test__/buttons.test.js +++ b/__test__/integration/buttons.test.js @@ -1,5 +1,5 @@ const $ = require('jquery'), - List = require('../src/index') + List = require('../../src/index') function fireKeyup(el) { if (document.createEvent) { diff --git a/__test__/integration/create.test.js b/__test__/integration/create.test.js new file mode 100644 index 00000000..8a353fbb --- /dev/null +++ b/__test__/integration/create.test.js @@ -0,0 +1,315 @@ +const $ = require('jquery') +const List = require('../../src/index') + +describe('Create', () => { + describe('With HTML items', () => { + let list, listEl + beforeEach(function () { + listEl = $(` +
    + +
    + `) + $(document.body).append(listEl) + list = new List('list', { valueNames: ['name'] }) + }) + afterEach(() => { + listEl.remove() + }) + + it('should contain one item', () => { + expect(list.items.length).toEqual(1) + expect(listEl.find('li').length).toEqual(1) + }) + + it('should contain two items', () => { + list.add({ name: 'Jonas' }) + expect(list.items.length).toEqual(2) + expect(listEl.find('li').length).toEqual(2) + }) + }) + + describe('With and element instead of id', () => { + var listEl, list + beforeEach(() => { + listEl = $(` +
    + +
    + `) + $(document.body).append(listEl) + var el = document.getElementById('list') + list = new List(el, { valueNames: ['name'] }) + }) + afterEach(() => { + listEl.remove() + }) + + it('should contain one item', () => { + expect(list.items.length).toEqual(1) + expect(listEl.find('li').length).toEqual(1) + }) + }) + + describe('Without items and with string template', () => { + var listEl, list + beforeEach(() => { + listEl = $('
    \ + \ +
    ') + $(document.body).append(listEl) + list = new List( + 'list', + { + valueNames: ['name'], + item: '
  • ', + }, + [{ name: 'Jonny' }] + ) + }) + afterEach(() => { + listEl.remove() + }) + + it('should contain one item', () => { + expect(list.items.length).toEqual(1) + expect(listEl.find('li').length).toEqual(1) + }) + + it('should contain two items', () => { + list.add({ name: 'Jonas' }) + expect(list.items.length).toEqual(2) + expect(listEl.find('li').length).toEqual(2) + }) + }) + + describe('Without items and with string template for table', () => { + var listEl, list + + beforeEach(() => { + listEl = $(` +
    +
    +
    + `) + $(document.body).append(listEl) + list = new List( + 'list', + { + valueNames: ['name'], + item: '', + }, + [{ name: 'Jonny' }] + ) + }) + + afterEach(() => { + listEl.remove() + }) + + it('should contain one item', () => { + expect(list.items.length).toEqual(1) + expect(listEl.find('tr').length).toEqual(1) + }) + + it('should contain two items', () => { + list.add({ name: 'Jonas' }) + expect(list.items.length).toEqual(2) + expect(listEl.find('tr').length).toEqual(2) + }) + }) + + describe('Without items and with template function', () => { + var listEl, list + beforeEach(() => { + listEl = $('
    ') + $(document.body).append(listEl) + list = new List( + 'list', + { + valueNames: ['name'], + item: function (values) { + return `
  • ` + }, + }, + [{ name: 'Jonny' }] + ) + }) + afterEach(() => { + listEl.remove() + }) + + it('should contain one item', () => { + expect(list.items.length).toEqual(1) + expect(listEl.find('li').length).toEqual(1) + }) + + it('should contain two items', () => { + list.add({ name: 'Jonas' }) + expect(list.items.length).toEqual(2) + expect(listEl.find('li').length).toEqual(2) + }) + + it('should get values from items', () => { + list.add({ name: 'Egon' }) + expect(listEl.find('li[data-template-fn-egon]').length).toEqual(1) + }) + }) + + describe('without items and or template', () => { + it('should throw error on init', () => { + var listEl = $('
    ') + $(document.body).append(listEl) + + expect(() => { + var list = new List('list', { + valueNames: ['name'], + }) + }).toThrow() + + listEl.remove() + }) + }) + + describe('Without items and with HTML template', () => { + var listEl, list, templateEl + beforeEach(() => { + listEl = $('
    ') + + templateEl = $('
  • ') + + $(document.body).append(listEl) + $(document.body).append(templateEl) + + list = new List( + 'list', + { + valueNames: ['name'], + item: 'template-item', + }, + [{ name: 'Jonny' }] + ) + }) + afterEach(() => { + listEl.remove() + templateEl.remove() + }) + + it('should contain one item', () => { + expect(list.items.length).toEqual(1) + expect(listEl.find('li').length).toEqual(1) + }) + + it('should contain two items', () => { + list.add({ name: 'Jonas' }) + expect(list.items.length).toEqual(2) + expect(listEl.find('li').length).toEqual(2) + }) + }) + + describe('Asyn index with existing list', () => { + it('should contain 162 items', (done) => { + var listEl = $(` +
    + +
    + `) + $(document.body).append(listEl) + var list = new List('list', { + valueNames: ['name'], + indexAsync: true, + parseComplete: function (list) { + expect(listEl.find('li').length).toEqual(162) + listEl.remove() + done() + }, + }) + }) + }) +}) diff --git a/__test__/defaults.test.js b/__test__/integration/defaults.test.js similarity index 100% rename from __test__/defaults.test.js rename to __test__/integration/defaults.test.js diff --git a/__test__/filter.test.js b/__test__/integration/filter.test.js similarity index 79% rename from __test__/filter.test.js rename to __test__/integration/filter.test.js index 37760e20..78711dc0 100644 --- a/__test__/filter.test.js +++ b/__test__/integration/filter.test.js @@ -1,4 +1,5 @@ const fixture = require('./fixtures') +const isVisible = require('../utils/is-visible') describe('Filter', function () { var list, jonny, martina, angelica, sebastian, imma, hasse @@ -68,34 +69,34 @@ describe('Filter', function () { }) }) it('should match jonny', function () { - expect(jonny.matching()).toBe(true) + expect(jonny.matching(list)).toBe(true) expect(jonny.filtered).toBe(true) - expect(jonny.visible()).toBe(true) + expect(isVisible(jonny.elm, list.list)).toBe(true) }) it('should match martina', function () { - expect(martina.matching()).toBe(true) + expect(martina.matching(list)).toBe(true) expect(martina.filtered).toBe(true) - expect(martina.visible()).toBe(true) + expect(isVisible(martina.elm, list.list)).toBe(true) }) it('should match but not show angelica', function () { - expect(angelica.matching()).toBe(true) + expect(angelica.matching(list)).toBe(true) expect(angelica.filtered).toBe(true) - expect(angelica.visible()).toBe(false) + expect(isVisible(angelica.elm, list.list)).toBe(false) }) it('should match but not show sebastian', function () { - expect(sebastian.matching()).toBe(true) + expect(sebastian.matching(list)).toBe(true) expect(sebastian.filtered).toBe(true) - expect(sebastian.visible()).toBe(false) + expect(isVisible(sebastian.elm, list.list)).toBe(false) }) it('should not match imma', function () { - expect(imma.matching()).toBe(false) + expect(imma.matching(list)).toBe(false) expect(imma.filtered).toBe(false) - expect(imma.visible()).toBe(false) + expect(isVisible(imma.elm, list.list)).toBe(false) }) it('should not match hasse', function () { - expect(hasse.matching()).toBe(false) + expect(hasse.matching(list)).toBe(false) expect(hasse.filtered).toBe(false) - expect(hasse.visible()).toBe(false) + expect(isVisible(hasse.elm, list.list)).toBe(false) }) }) }) diff --git a/__test__/fixtures-fuzzysearch.js b/__test__/integration/fixtures-fuzzysearch.js similarity index 100% rename from __test__/fixtures-fuzzysearch.js rename to __test__/integration/fixtures-fuzzysearch.js diff --git a/__test__/fixtures-pagination.js b/__test__/integration/fixtures-pagination.js similarity index 100% rename from __test__/fixtures-pagination.js rename to __test__/integration/fixtures-pagination.js diff --git a/__test__/fixtures.js b/__test__/integration/fixtures.js similarity index 96% rename from __test__/fixtures.js rename to __test__/integration/fixtures.js index 083dae56..32245c99 100644 --- a/__test__/fixtures.js +++ b/__test__/integration/fixtures.js @@ -1,5 +1,5 @@ const $ = require('jquery'), - List = require('../src/index') + List = require('../../src/index') var fixture = { list: function (valueNames, items) { diff --git a/__test__/fuzzysearch.test.js b/__test__/integration/fuzzysearch.test.js similarity index 98% rename from __test__/fuzzysearch.test.js rename to __test__/integration/fuzzysearch.test.js index eea89f5d..f0ba9afc 100644 --- a/__test__/fuzzysearch.test.js +++ b/__test__/integration/fuzzysearch.test.js @@ -1,6 +1,6 @@ const $ = require('jquery'), fixtureFuzzysearch = require('./fixtures-fuzzysearch'), - List = require('../src/index') + List = require('../../src/index') function fireKeyup(el) { if (document.createEvent) { diff --git a/__test__/item.test.js b/__test__/integration/item.test.js similarity index 73% rename from __test__/item.test.js rename to __test__/integration/item.test.js index e1e6c464..07131c51 100644 --- a/__test__/item.test.js +++ b/__test__/integration/item.test.js @@ -1,5 +1,6 @@ const $ = require('jquery'), - fixture = require('./fixtures') + fixture = require('./fixtures'), + isVisible = require('../utils/is-visible') describe('Item', function () { var list, item @@ -39,11 +40,8 @@ describe('Item', function () { }) it('should have all default methods', function () { - expect(item.hide).toBeInstanceOf(Function) - expect(item.show).toBeInstanceOf(Function) expect(item.values).toBeInstanceOf(Function) expect(item.matching).toBeInstanceOf(Function) - expect(item.visible).toBeInstanceOf(Function) }) }) @@ -79,44 +77,28 @@ describe('Item', function () { }) }) - describe('Hide, show, visible', function () { - it('should be hidden', function () { - expect($('#list li').length).toEqual(1) - item.hide() - expect(item.visible()).toBe(false) - expect($('#list li').length).toEqual(0) - }) - it('should be visible', function () { - item.hide() - expect($('#list li').length).toEqual(0) - item.show() - expect(item.visible()).toBe(true) - expect($('#list li').length).toEqual(1) - }) - }) - describe('Matching, found, filtered', function () { describe('Searching', function () { it('should not be visible, match, found or filtered', function () { list.search('Fredrik') - expect(item.matching()).toBe(false) + expect(item.matching(list)).toBe(false) expect(item.found).toBe(false) expect(item.filtered).toBe(false) - expect(item.visible()).toBe(false) + expect(isVisible(item.elm, list.list)).toBe(false) }) it('should be visble, match and found but not filterd', function () { var result = list.search('Sven') - expect(item.matching()).toBe(true) + expect(item.matching(list)).toBe(true) expect(item.found).toBe(true) expect(item.filtered).toBe(false) - expect(item.visible()).toBe(true) + expect(isVisible(item.elm, list.list)).toBe(true) }) it('reset: should be visible and matching but not found or filtered', function () { list.search() - expect(item.matching()).toBe(true) + expect(item.matching(list)).toBe(true) expect(item.found).toBe(false) expect(item.filtered).toBe(false) - expect(item.visible()).toBe(true) + expect(isVisible(item.elm, list.list)).toBe(true) }) }) describe('Filtering', function () { @@ -124,26 +106,26 @@ describe('Item', function () { list.filter(function (item) { return item.values().name == 'Fredrik' }) - expect(item.matching()).toBe(false) + expect(item.matching(list)).toBe(false) expect(item.found).toBe(false) expect(item.filtered).toBe(false) - expect(item.visible()).toBe(false) + expect(isVisible(item.elm, list.list)).toBe(false) }) it('should be visble, match and filtered but not found', function () { list.filter(function (item) { return item.values().name == 'Sven' }) - expect(item.matching()).toBe(true) + expect(item.matching(list)).toBe(true) expect(item.found).toBe(false) expect(item.filtered).toBe(true) - expect(item.visible()).toBe(true) + expect(isVisible(item.elm, list.list)).toBe(true) }) it('reset: should be visble and match but not filtered or found', function () { list.filter() - expect(item.matching()).toBe(true) + expect(item.matching(list)).toBe(true) expect(item.found).toBe(false) expect(item.filtered).toBe(false) - expect(item.visible()).toBe(true) + expect(isVisible(item.elm, list.list)).toBe(true) }) }) }) diff --git a/__test__/off.test.js b/__test__/integration/off.test.js similarity index 100% rename from __test__/off.test.js rename to __test__/integration/off.test.js diff --git a/__test__/on.test.js b/__test__/integration/on.test.js similarity index 100% rename from __test__/on.test.js rename to __test__/integration/on.test.js diff --git a/__test__/pagination.test.js b/__test__/integration/pagination.test.js similarity index 99% rename from __test__/pagination.test.js rename to __test__/integration/pagination.test.js index 0e113b1e..3dc1ff3e 100644 --- a/__test__/pagination.test.js +++ b/__test__/integration/pagination.test.js @@ -1,6 +1,6 @@ const $ = require('jquery'), fixturePagination = require('./fixtures-pagination'), - List = require('../src/index') + List = require('../../src/index') describe('Pagination', function () { describe('Default settings, innerWindow: 2, outerWindow: 0, left: 0, right: 0', function () { diff --git a/__test__/parse.test.js b/__test__/integration/parse.test.js similarity index 99% rename from __test__/parse.test.js rename to __test__/integration/parse.test.js index 4055050f..64c7e290 100644 --- a/__test__/parse.test.js +++ b/__test__/integration/parse.test.js @@ -1,5 +1,5 @@ const $ = require('jquery'), - List = require('../src/index') + List = require('../../src/index') describe('Parse', function () { describe('Parse class', function () { diff --git a/__test__/re-index.test.js b/__test__/integration/re-index.test.js similarity index 100% rename from __test__/re-index.test.js rename to __test__/integration/re-index.test.js diff --git a/__test__/search-filter.test.js b/__test__/integration/search-filter.test.js similarity index 62% rename from __test__/search-filter.test.js rename to __test__/integration/search-filter.test.js index 73e7264f..e7f69b60 100644 --- a/__test__/search-filter.test.js +++ b/__test__/integration/search-filter.test.js @@ -29,12 +29,12 @@ describe('Search and filter', function () { return item.values().born == '1986' }) expect(list.matchingItems.length).toEqual(3) - expect(jonny.matching()).toBe(true) - expect(martina.matching()).toBe(true) - expect(angelica.matching()).toBe(true) - expect(sebastian.matching()).toBe(false) - expect(imma.matching()).toBe(false) - expect(hasse.matching()).toBe(false) + expect(jonny.matching(list)).toBe(true) + expect(martina.matching(list)).toBe(true) + expect(angelica.matching(list)).toBe(true) + expect(sebastian.matching(list)).toBe(false) + expect(imma.matching(list)).toBe(false) + expect(hasse.matching(list)).toBe(false) }) it('should find everyone born 1986 and containes "ö"', function () { list.filter(function (item) { @@ -42,12 +42,12 @@ describe('Search and filter', function () { }) list.search('ö') expect(list.matchingItems.length).toEqual(1) - expect(jonny.matching()).toBe(true) - expect(martina.matching()).toBe(false) - expect(angelica.matching()).toBe(false) - expect(sebastian.matching()).toBe(false) - expect(imma.matching()).toBe(false) - expect(hasse.matching()).toBe(false) + expect(jonny.matching(list)).toBe(true) + expect(martina.matching(list)).toBe(false) + expect(angelica.matching(list)).toBe(false) + expect(sebastian.matching(list)).toBe(false) + expect(imma.matching(list)).toBe(false) + expect(hasse.matching(list)).toBe(false) }) it('should find everyone with a "ö"', function () { list.filter(function (item) { @@ -56,12 +56,12 @@ describe('Search and filter', function () { list.search('ö') list.filter() expect(list.matchingItems.length).toEqual(4) - expect(jonny.matching()).toBe(true) - expect(martina.matching()).toBe(false) - expect(angelica.matching()).toBe(false) - expect(sebastian.matching()).toBe(true) - expect(imma.matching()).toBe(true) - expect(hasse.matching()).toBe(true) + expect(jonny.matching(list)).toBe(true) + expect(martina.matching(list)).toBe(false) + expect(angelica.matching(list)).toBe(false) + expect(sebastian.matching(list)).toBe(true) + expect(imma.matching(list)).toBe(true) + expect(hasse.matching(list)).toBe(true) }) }) }) diff --git a/__test__/search.test.js b/__test__/integration/search.test.js old mode 100644 new mode 100755 similarity index 68% rename from __test__/search.test.js rename to __test__/integration/search.test.js index b819be16..b2af0e62 --- a/__test__/search.test.js +++ b/__test__/integration/search.test.js @@ -30,22 +30,22 @@ describe('Search', function () { it('should find jonny, martina, angelice', function () { var result = list.search('1986') expect(result.length).toEqual(3) // 3!! - expect(jonny.matching()).toBe(true) - expect(martina.matching()).toBe(true) - expect(angelica.matching()).toBe(true) - expect(sebastian.matching()).toBe(false) - expect(imma.matching()).toBe(false) - expect(hasse.matching()).toBe(false) + expect(jonny.matching(list)).toBe(true) + expect(martina.matching(list)).toBe(true) + expect(angelica.matching(list)).toBe(true) + expect(sebastian.matching(list)).toBe(false) + expect(imma.matching(list)).toBe(false) + expect(hasse.matching(list)).toBe(false) }) it('should find all with utf-8 char ö', function () { var result = list.search('ö') expect(result.length).toEqual(4) // 4!! - expect(jonny.matching()).toBe(true) - expect(martina.matching()).toBe(false) - expect(angelica.matching()).toBe(false) - expect(sebastian.matching()).toBe(true) - expect(imma.matching()).toBe(true) - expect(hasse.matching()).toBe(true) + expect(jonny.matching(list)).toBe(true) + expect(martina.matching(list)).toBe(false) + expect(angelica.matching(list)).toBe(false) + expect(sebastian.matching(list)).toBe(true) + expect(imma.matching(list)).toBe(true) + expect(hasse.matching(list)).toBe(true) }) it('should not break with weird searches', function () { expect(function () { @@ -106,7 +106,7 @@ describe('Search', function () { }) }) - describe('Specfic columns', function () { + describe('Specific columns', function () { it('should find match in column', function () { var result = list.search('jonny', ['name']) expect(result.length).toEqual(1) @@ -125,11 +125,11 @@ describe('Search', function () { var result = list.search('jonny', ['born']) expect(result.length).toEqual(0) }) - it('should work with columns that does not exist', function () { + it('should work with columns that do not exist', function () { var result = list.search('jonny', ['pet']) expect(result.length).toEqual(0) }) - it('should remove columnm option', function () { + it('should remove column option', function () { var result = list.search('jonny', ['born']) expect(result.length).toEqual(0) result = list.search('jonny') @@ -154,6 +154,48 @@ describe('Search', function () { expect(result.length).toEqual(4) }) }) + + describe('Multiple word search', function () { + it('should find jonny, hasse', function () { + var result = list.search('berg str') + expect(result.length).toEqual(2) + expect(jonny.matching(list)).toBe(true) + expect(martina.matching(list)).toBe(false) + expect(angelica.matching(list)).toBe(false) + expect(sebastian.matching(list)).toBe(false) + expect(imma.matching(list)).toBe(false) + expect(hasse.matching(list)).toBe(true) + }) + it('should find martina, angelica, sebastian, hasse', function () { + var result = list.search('a e') + expect(result.length).toEqual(4) + expect(jonny.matching(list)).toBe(false) + expect(martina.matching(list)).toBe(true) + expect(angelica.matching(list)).toBe(true) + expect(sebastian.matching(list)).toBe(true) + expect(imma.matching(list)).toBe(false) + expect(hasse.matching(list)).toBe(true) + }) + it('stripping whitespace should find martina', function () { + var result = list.search('martina elm ') + expect(result.length).toEqual(1) + expect(result[0]).toEqual(martina) + }) + }) + + describe('Quoted phrase searches', function () { + it('should find martina', function () { + var result = list.search('"a e"') + expect(result.length).toEqual(1) + expect(result[0]).toEqual(martina) + }) + it('quoted phrase and multiple words should find jonny', function () { + var result = list.search('" str" 1986') + expect(result.length).toEqual(1) + expect(result[0]).toEqual(jonny) + }) + }) + // // describe('Special characters', function() { // it('should escape and handle special characters', function() { diff --git a/__test__/integration/show.test.js b/__test__/integration/show.test.js new file mode 100644 index 00000000..d32dcf46 --- /dev/null +++ b/__test__/integration/show.test.js @@ -0,0 +1,213 @@ +const fixture = require('./fixtures') +const isVisible = require('../utils/is-visible') + +describe('Show', function () { + var list, a, b, c, d, e, f + + beforeAll(function () { + list = fixture.list( + ['id', 'id2'], + [ + { id: '1', id2: 'a' }, + { id: '2', id2: 'a' }, + { id: '3', id2: 'b' }, + { id: '4', id2: 'b' }, + { id: '5', id2: 'bc' }, + { id: '6', id2: 'bc' }, + ] + ) + a = list.get('id', '1')[0] + b = list.get('id', '2')[0] + c = list.get('id', '3')[0] + d = list.get('id', '4')[0] + e = list.get('id', '5')[0] + f = list.get('id', '6')[0] + }) + + afterAll(function () { + fixture.removeList() + }) + + afterEach(function () { + list.filter() + list.show(1, 200) + }) + + describe('Basics', function () { + it('should be 1, 2', function () { + list.show(1, 2) + expect(list.visibleItems.length).toEqual(2) + expect(isVisible(a.elm, list.list)).toBe(true) + expect(isVisible(b.elm, list.list)).toBe(true) + expect(isVisible(c.elm, list.list)).toBe(false) + expect(isVisible(d.elm, list.list)).toBe(false) + expect(isVisible(e.elm, list.list)).toBe(false) + expect(isVisible(f.elm, list.list)).toBe(false) + }) + it('should show item 6', function () { + list.show(6, 2) + expect(list.visibleItems.length).toEqual(1) + expect(isVisible(a.elm, list.list)).toBe(false) + expect(isVisible(b.elm, list.list)).toBe(false) + expect(isVisible(c.elm, list.list)).toBe(false) + expect(isVisible(d.elm, list.list)).toBe(false) + expect(isVisible(e.elm, list.list)).toBe(false) + expect(isVisible(f.elm, list.list)).toBe(true) + }) + it('should show item 1, 2, 3, 4, 5, 6', function () { + list.show(1, 200) + expect(list.visibleItems.length).toEqual(6) + expect(isVisible(a.elm, list.list)).toBe(true) + expect(isVisible(b.elm, list.list)).toBe(true) + expect(isVisible(c.elm, list.list)).toBe(true) + expect(isVisible(d.elm, list.list)).toBe(true) + expect(isVisible(e.elm, list.list)).toBe(true) + expect(isVisible(f.elm, list.list)).toBe(true) + }) + it('should show item 3, 4, 5', function () { + list.show(3, 3) + expect(list.visibleItems.length).toEqual(3) + expect(isVisible(a.elm, list.list)).toBe(false) + expect(isVisible(b.elm, list.list)).toBe(false) + expect(isVisible(c.elm, list.list)).toBe(true) + expect(isVisible(d.elm, list.list)).toBe(true) + expect(isVisible(e.elm, list.list)).toBe(true) + expect(isVisible(f.elm, list.list)).toBe(false) + }) + it('should show item 5, 6', function () { + list.show(5, 3) + expect(list.visibleItems.length).toEqual(2) + expect(isVisible(a.elm, list.list)).toBe(false) + expect(isVisible(b.elm, list.list)).toBe(false) + expect(isVisible(c.elm, list.list)).toBe(false) + expect(isVisible(d.elm, list.list)).toBe(false) + expect(isVisible(e.elm, list.list)).toBe(true) + expect(isVisible(f.elm, list.list)).toBe(true) + }) + }) + + describe('Search', function () { + afterEach(function () { + list.search() + }) + it('should show 3, 4', function () { + list.search('b') + list.show(1, 2) + expect(list.visibleItems.length).toEqual(2) + expect(isVisible(a.elm, list.list)).toBe(false) + expect(isVisible(b.elm, list.list)).toBe(false) + expect(isVisible(c.elm, list.list)).toBe(true) + expect(isVisible(d.elm, list.list)).toBe(true) + expect(isVisible(e.elm, list.list)).toBe(false) + expect(isVisible(f.elm, list.list)).toBe(false) + }) + it('should show item 3,4,5,6', function () { + list.search('b') + list.show(1, 4) + expect(list.visibleItems.length).toEqual(4) + expect(isVisible(a.elm, list.list)).toBe(false) + expect(isVisible(b.elm, list.list)).toBe(false) + expect(isVisible(c.elm, list.list)).toBe(true) + expect(isVisible(d.elm, list.list)).toBe(true) + expect(isVisible(e.elm, list.list)).toBe(true) + expect(isVisible(f.elm, list.list)).toBe(true) + }) + it('should not show any items but match two', function () { + list.search('a') + list.show(3, 2) + expect(list.visibleItems.length).toEqual(0) + expect(list.matchingItems.length).toEqual(2) + expect(isVisible(a.elm, list.list)).toBe(false) + expect(isVisible(b.elm, list.list)).toBe(false) + expect(isVisible(c.elm, list.list)).toBe(false) + expect(isVisible(d.elm, list.list)).toBe(false) + expect(isVisible(e.elm, list.list)).toBe(false) + expect(isVisible(f.elm, list.list)).toBe(false) + }) + }) + + describe('Filter', function () { + afterEach(function () { + list.filter() + }) + it('should show 3, 4', function () { + list.filter(function (item) { + return item.values().id2 == 'b' + }) + list.show(1, 2) + expect(list.visibleItems.length).toEqual(2) + expect(list.matchingItems.length).toEqual(2) + expect(isVisible(a.elm, list.list)).toBe(false) + expect(isVisible(b.elm, list.list)).toBe(false) + expect(isVisible(c.elm, list.list)).toBe(true) + expect(isVisible(d.elm, list.list)).toBe(true) + expect(isVisible(e.elm, list.list)).toBe(false) + expect(isVisible(f.elm, list.list)).toBe(false) + }) + it('should show item 3,4,5,6', function () { + list.filter(function (item) { + return item.values().id2 == 'bc' + }) + list.show(1, 4) + expect(list.visibleItems.length).toEqual(2) + expect(list.matchingItems.length).toEqual(2) + expect(isVisible(a.elm, list.list)).toBe(false) + expect(isVisible(b.elm, list.list)).toBe(false) + expect(isVisible(c.elm, list.list)).toBe(false) + expect(isVisible(d.elm, list.list)).toBe(false) + expect(isVisible(e.elm, list.list)).toBe(true) + expect(isVisible(f.elm, list.list)).toBe(true) + }) + it('should not show any items but match two', function () { + list.filter(function (item) { + return item.values().id2 == 'b' + }) + list.show(3, 2) + expect(list.visibleItems.length).toEqual(0) + expect(list.matchingItems.length).toEqual(2) + expect(isVisible(a.elm, list.list)).toBe(false) + expect(isVisible(b.elm, list.list)).toBe(false) + expect(isVisible(c.elm, list.list)).toBe(false) + expect(isVisible(d.elm, list.list)).toBe(false) + expect(isVisible(e.elm, list.list)).toBe(false) + expect(isVisible(f.elm, list.list)).toBe(false) + }) + }) + + describe('Filter and search', function () { + afterEach(function () { + list.filter() + }) + it('should show 4, 5', function () { + list.show(1, 2) + list.filter(function (item) { + return item.values().id > '3' + }) + list.search('b') + expect(list.visibleItems.length).toEqual(2) + expect(list.matchingItems.length).toEqual(3) + expect(isVisible(a.elm, list.list)).toBe(false) + expect(isVisible(b.elm, list.list)).toBe(false) + expect(isVisible(c.elm, list.list)).toBe(false) + expect(isVisible(d.elm, list.list)).toBe(true) + expect(isVisible(e.elm, list.list)).toBe(true) + expect(isVisible(f.elm, list.list)).toBe(false) + }) + it('should show 5, 6', function () { + list.show(1, 2) + list.filter(function (item) { + return item.values().id > '3' + }) + list.search('b') + list.show(2, 2) + expect(list.visibleItems.length).toEqual(2) + expect(list.matchingItems.length).toEqual(3) + expect(isVisible(a.elm, list.list)).toBe(false) + expect(isVisible(b.elm, list.list)).toBe(false) + expect(isVisible(c.elm, list.list)).toBe(false) + expect(isVisible(d.elm, list.list)).toBe(false) + expect(isVisible(e.elm, list.list)).toBe(true) + expect(isVisible(f.elm, list.list)).toBe(true) + }) + }) +}) diff --git a/__test__/sort.test.js b/__test__/integration/sort.test.js similarity index 97% rename from __test__/sort.test.js rename to __test__/integration/sort.test.js index c30503f8..bcbcdd1c 100644 --- a/__test__/sort.test.js +++ b/__test__/integration/sort.test.js @@ -286,24 +286,20 @@ describe('Sort', function () { expect(list.items[11].values().val).toBe('z') }) - it('should handle not longer (since 1.4.0) space and zero the same for desc and asc', function () { + it('should handle space and zero the same for desc and asc (worked 1.4.0 - 2.3.0 then string-natural-compare was updated)', function () { list.clear() list.add({ val: '' }) list.add({ val: '0' }) - list.add({ val: 0 }) list.sort('val', { order: 'asc' }) expect(list.items[0].values().val).toBe('') expect(list.items[1].values().val).toBe('0') - expect(list.items[2].values().val).toBe(0) list.sort('val', { order: 'desc' }) - expect(list.items[0].values().val).toBe('0') - expect(list.items[1].values().val).toBe(0) - expect(list.items[2].values().val).toBe('') + expect(list.items[0].values().val).toBe('') + expect(list.items[1].values().val).toBe('0') list.sort('val', { order: 'asc' }) expect(list.items[0].values().val).toBe('') expect(list.items[1].values().val).toBe('0') - expect(list.items[2].values().val).toBe(0) }) }) diff --git a/__test__/trigger.test.js b/__test__/integration/trigger.test.js similarity index 100% rename from __test__/trigger.test.js rename to __test__/integration/trigger.test.js diff --git a/__test__/utils.classes.test.js b/__test__/integration/utils.classes.test.js similarity index 96% rename from __test__/utils.classes.test.js rename to __test__/integration/utils.classes.test.js index e2ad006f..71038e7d 100644 --- a/__test__/utils.classes.test.js +++ b/__test__/integration/utils.classes.test.js @@ -1,4 +1,4 @@ -const classes = require('../src/utils/classes') +const classes = require('../../src/utils/classes') describe('Classes', function () { var el diff --git a/__test__/utils.get-by-class.test.js b/__test__/integration/utils.get-by-class.test.js similarity index 92% rename from __test__/utils.get-by-class.test.js rename to __test__/integration/utils.get-by-class.test.js index b524b544..b3f08d95 100644 --- a/__test__/utils.get-by-class.test.js +++ b/__test__/integration/utils.get-by-class.test.js @@ -1,4 +1,4 @@ -const getByClass = require('../src/utils/get-by-class') +const getByClass = require('../../src/utils/get-by-class') describe('GetByClass', function () { var el diff --git a/__test__/show.test.js b/__test__/show.test.js deleted file mode 100644 index 60fa41cb..00000000 --- a/__test__/show.test.js +++ /dev/null @@ -1,212 +0,0 @@ -const fixture = require('./fixtures') - -describe('Show', function () { - var list, a, b, c, d, e, f - - beforeAll(function () { - list = fixture.list( - ['id', 'id2'], - [ - { id: '1', id2: 'a' }, - { id: '2', id2: 'a' }, - { id: '3', id2: 'b' }, - { id: '4', id2: 'b' }, - { id: '5', id2: 'bc' }, - { id: '6', id2: 'bc' }, - ] - ) - a = list.get('id', '1')[0] - b = list.get('id', '2')[0] - c = list.get('id', '3')[0] - d = list.get('id', '4')[0] - e = list.get('id', '5')[0] - f = list.get('id', '6')[0] - }) - - afterAll(function () { - fixture.removeList() - }) - - afterEach(function () { - list.filter() - list.show(1, 200) - }) - - describe('Basics', function () { - it('should be 1, 2', function () { - list.show(1, 2) - expect(list.visibleItems.length).toEqual(2) - expect(a.visible()).toBe(true) - expect(b.visible()).toBe(true) - expect(c.visible()).toBe(false) - expect(d.visible()).toBe(false) - expect(e.visible()).toBe(false) - expect(f.visible()).toBe(false) - }) - it('should show item 6', function () { - list.show(6, 2) - expect(list.visibleItems.length).toEqual(1) - expect(a.visible()).toBe(false) - expect(b.visible()).toBe(false) - expect(c.visible()).toBe(false) - expect(d.visible()).toBe(false) - expect(e.visible()).toBe(false) - expect(f.visible()).toBe(true) - }) - it('should show item 1, 2, 3, 4, 5, 6', function () { - list.show(1, 200) - expect(list.visibleItems.length).toEqual(6) - expect(a.visible()).toBe(true) - expect(b.visible()).toBe(true) - expect(c.visible()).toBe(true) - expect(d.visible()).toBe(true) - expect(e.visible()).toBe(true) - expect(f.visible()).toBe(true) - }) - it('should show item 3, 4, 5', function () { - list.show(3, 3) - expect(list.visibleItems.length).toEqual(3) - expect(a.visible()).toBe(false) - expect(b.visible()).toBe(false) - expect(c.visible()).toBe(true) - expect(d.visible()).toBe(true) - expect(e.visible()).toBe(true) - expect(f.visible()).toBe(false) - }) - it('should show item 5, 6', function () { - list.show(5, 3) - expect(list.visibleItems.length).toEqual(2) - expect(a.visible()).toBe(false) - expect(b.visible()).toBe(false) - expect(c.visible()).toBe(false) - expect(d.visible()).toBe(false) - expect(e.visible()).toBe(true) - expect(f.visible()).toBe(true) - }) - }) - - describe('Search', function () { - afterEach(function () { - list.search() - }) - it('should show 3, 4', function () { - list.search('b') - list.show(1, 2) - expect(list.visibleItems.length).toEqual(2) - expect(a.visible()).toBe(false) - expect(b.visible()).toBe(false) - expect(c.visible()).toBe(true) - expect(d.visible()).toBe(true) - expect(e.visible()).toBe(false) - expect(f.visible()).toBe(false) - }) - it('should show item 3,4,5,6', function () { - list.search('b') - list.show(1, 4) - expect(list.visibleItems.length).toEqual(4) - expect(a.visible()).toBe(false) - expect(b.visible()).toBe(false) - expect(c.visible()).toBe(true) - expect(d.visible()).toBe(true) - expect(e.visible()).toBe(true) - expect(f.visible()).toBe(true) - }) - it('should not show any items but match two', function () { - list.search('a') - list.show(3, 2) - expect(list.visibleItems.length).toEqual(0) - expect(list.matchingItems.length).toEqual(2) - expect(a.visible()).toBe(false) - expect(b.visible()).toBe(false) - expect(c.visible()).toBe(false) - expect(d.visible()).toBe(false) - expect(e.visible()).toBe(false) - expect(f.visible()).toBe(false) - }) - }) - - describe('Filter', function () { - afterEach(function () { - list.filter() - }) - it('should show 3, 4', function () { - list.filter(function (item) { - return item.values().id2 == 'b' - }) - list.show(1, 2) - expect(list.visibleItems.length).toEqual(2) - expect(list.matchingItems.length).toEqual(2) - expect(a.visible()).toBe(false) - expect(b.visible()).toBe(false) - expect(c.visible()).toBe(true) - expect(d.visible()).toBe(true) - expect(e.visible()).toBe(false) - expect(f.visible()).toBe(false) - }) - it('should show item 3,4,5,6', function () { - list.filter(function (item) { - return item.values().id2 == 'bc' - }) - list.show(1, 4) - expect(list.visibleItems.length).toEqual(2) - expect(list.matchingItems.length).toEqual(2) - expect(a.visible()).toBe(false) - expect(b.visible()).toBe(false) - expect(c.visible()).toBe(false) - expect(d.visible()).toBe(false) - expect(e.visible()).toBe(true) - expect(f.visible()).toBe(true) - }) - it('should not show any items but match two', function () { - list.filter(function (item) { - return item.values().id2 == 'b' - }) - list.show(3, 2) - expect(list.visibleItems.length).toEqual(0) - expect(list.matchingItems.length).toEqual(2) - expect(a.visible()).toBe(false) - expect(b.visible()).toBe(false) - expect(c.visible()).toBe(false) - expect(d.visible()).toBe(false) - expect(e.visible()).toBe(false) - expect(f.visible()).toBe(false) - }) - }) - - describe('Filter and search', function () { - afterEach(function () { - list.filter() - }) - it('should show 4, 5', function () { - list.show(1, 2) - list.filter(function (item) { - return item.values().id > '3' - }) - list.search('b') - expect(list.visibleItems.length).toEqual(2) - expect(list.matchingItems.length).toEqual(3) - expect(a.visible()).toBe(false) - expect(b.visible()).toBe(false) - expect(c.visible()).toBe(false) - expect(d.visible()).toBe(true) - expect(e.visible()).toBe(true) - expect(f.visible()).toBe(false) - }) - it('should show 5, 6', function () { - list.show(1, 2) - list.filter(function (item) { - return item.values().id > '3' - }) - list.search('b') - list.show(2, 2) - expect(list.visibleItems.length).toEqual(2) - expect(list.matchingItems.length).toEqual(3) - expect(a.visible()).toBe(false) - expect(b.visible()).toBe(false) - expect(c.visible()).toBe(false) - expect(d.visible()).toBe(false) - expect(e.visible()).toBe(true) - expect(f.visible()).toBe(true) - }) - }) -}) diff --git a/__test__/unit/item.test.js b/__test__/unit/item.test.js new file mode 100644 index 00000000..e4f1ce15 --- /dev/null +++ b/__test__/unit/item.test.js @@ -0,0 +1,102 @@ +const $ = require('jquery') + +const Item = require('../../src/item') +const templater = require('../../src/templater') + +describe('item', function () { + beforeAll(() => { + this.templateParams = { + template: '
    ', + valueNames: ['name'], + } + }) + describe('init', () => { + it('should throw error if missing template', () => { + expect(() => { + new Item({ name: 'Jonny' }) + }).toThrowError('missing_item_template') + }) + }) + describe('create', () => { + it('should create item with only values', () => { + const template = templater.getTemplate(this.templateParams) + const item = new Item({ name: 'Jonny' }, { template }) + expect(item._values).toEqual({ name: 'Jonny' }) + expect(item.elm).toBeUndefined() + }) + it('should create item with values and element', () => { + const template = templater.getTemplate(this.templateParams) + const element = document.createElement('div') + const item = new Item({ name: 'Jonny' }, { element, template }) + expect(item._values).toEqual({ name: 'Jonny' }) + expect(item.elm).not.toBeUndefined() + expect(item.elm).toBe(element) + }) + }) + describe('get', () => { + it('should get values', () => { + const template = templater.getTemplate(this.templateParams) + const item = new Item({ name: 'Jonny' }, { template }) + expect(item.values()).toEqual({ name: 'Jonny' }) + }) + }) + describe('set', () => { + it('should set values on item without element', () => { + const template = templater.getTemplate(this.templateParams) + const item = new Item({ name: 'Jonny' }, { template }) + item.values({ name: 'Anna' }) + expect(item.elm).toBeUndefined() + expect(item._values.name).toEqual('Anna') + }) + it('should set values on item with element', () => { + const template = templater.getTemplate(this.templateParams) + const element = $('
    Jonny
    ')[0] + const item = new Item({ name: 'Jonny' }, { element, template }) + expect(item.elm.querySelector('span').innerHTML).toEqual('Jonny') + item.values({ name: 'Anna' }) + expect(item._values.name).toEqual('Anna') + expect(item.elm.querySelector('span').innerHTML).toEqual('Anna') + }) + }) + describe('matching', () => { + beforeAll(() => { + const template = templater.getTemplate(this.templateParams) + this.item = new Item({ name: 'Jonny' }, { template }) + }) + it('should handle not found or filtered', () => { + const item = this.item + item.found = false + item.filtered = false + expect(item.matching({ searched: false, filtered: false })).toEqual(true) + expect(item.matching({ searched: true, filtered: false })).toEqual(false) + expect(item.matching({ searched: true, filtered: false })).toEqual(false) + }) + it('should handle found and not filtered', () => { + const item = this.item + item.found = true + item.filtered = false + expect(item.matching({ searched: false, filtered: false })).toEqual(true) + expect(item.matching({ searched: true, filtered: false })).toEqual(true) + expect(item.matching({ searched: true, filtered: true })).toEqual(false) + expect(item.matching({ searched: false, filtered: true })).toEqual(false) + }) + it('should handle not found but filtered', () => { + const item = this.item + item.found = false + item.filtered = true + expect(item.matching({ searched: false, filtered: false })).toEqual(true) + expect(item.matching({ searched: true, filtered: false })).toEqual(false) + expect(item.matching({ searched: true, filtered: true })).toEqual(false) + expect(item.matching({ searched: false, filtered: true })).toEqual(true) + }) + it('should handle both found and filtered', () => { + const item = this.item + item.found = true + item.filtered = true + expect(item.matching({ searched: false, filtered: false })).toEqual(true) + expect(item.matching({ searched: true, filtered: false })).toEqual(true) + expect(item.matching({ searched: true, filtered: true })).toEqual(true) + expect(item.matching({ searched: false, filtered: true })).toEqual(true) + }) + }) +}) diff --git a/__test__/unit/sort-buttons.test.js b/__test__/unit/sort-buttons.test.js new file mode 100644 index 00000000..5666f293 --- /dev/null +++ b/__test__/unit/sort-buttons.test.js @@ -0,0 +1,210 @@ +const naturalSort = require('string-natural-compare') +const $ = require('jquery') + +const Item = require('../../src/item') +const templater = require('../../src/templater') +const { + addSortListeners, + getInSensitive, + getNextSortOrder, + setSortOrder, + clearSortOrder, +} = require('../../src/sort-buttons') + +describe('sort listeners', function () { + describe('getInSensitive', () => { + it('should be false if data-insensitive is false', () => { + const button = $(`