123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- import {plaid} from "@/builder";
- describe('builder', () => {
- it('pushState with object will merge into url queries', () => {
- const b = plaid().
- eventFunc("hello").
- query("name", "felix").
- mergeQuery(true).
- url("/page1?hello=1&page=2#scroll=123_0")
- expect(b.buildFetchURL()).toEqual('/page1?__execute_event__=hello&hello=1&name=felix&page=2');
- const [pushedData, title, url] = b.buildPushStateArgs()
- expect(url).toEqual('/page1?hello=1&name=felix&page=2#scroll=123_0');
- expect(pushedData).toEqual({ query: { hello: '1', name: 'felix', page: '2' }, url: '/page1?hello=1&name=felix&page=2#scroll=123_0' });
- });
- it('pushState with string will replace url queries', () => {
- const b = plaid().
- eventFunc("hello").
- query("name", "felix").
- url("/page1?hello=1&page=2")
- expect(b.buildFetchURL()).toEqual('/page1?__execute_event__=hello&name=felix');
- const [pushedData, title, url] = b.buildPushStateArgs()
- expect(url).toEqual('/page1?name=felix');
- expect(pushedData).toEqual({ query: { name: 'felix' }, url: '/page1?name=felix' });
- });
- it('pushState with clearMergeQuery will delete provided keys then mergeQuery', () => {
- const b = plaid().
- url("/page1?active_filter_tab=missing_value&missing_value=1&approved.gte=1642003200&order_by=CreatedAt_ASC").
- stringQuery("missing_value=2&channel=2").
- query("name", "felix").
- clearMergeQuery(["missing_value", "channel", "approved"])
- const [pushedData, title, url] = b.buildPushStateArgs()
- expect(url).toEqual('/page1?active_filter_tab=missing_value&channel=2&missing_value=2&name=felix&order_by=CreatedAt_ASC');
- expect(pushedData).toEqual({ query: { active_filter_tab: 'missing_value', channel: '2', name: "felix", missing_value: '2', order_by: 'CreatedAt_ASC'}, url: '/page1?active_filter_tab=missing_value&channel=2&missing_value=2&name=felix&order_by=CreatedAt_ASC' });
- });
- it('pushState with stringQuery empty will delete provided keys', () => {
- const b = plaid().
- url("/page1?active_filter_tab=missing_value&missing_value=1&approved.gte=1642003200&order_by=CreatedAt_ASC").
- clearMergeQuery(["missing_value", "channel", "approved"])
- const [pushedData, title, url] = b.buildPushStateArgs()
- expect(url).toEqual('/page1?active_filter_tab=missing_value&order_by=CreatedAt_ASC');
- });
- it('add operator will add to current query values', () => {
- const b = plaid().
- eventFunc("hello").
- query("selectedIds", { value: '5', add: true }).
- mergeQuery(true).
- url("/page1?selectedIds=1,2,3&page=2")
- expect(b.buildFetchURL()).toEqual('/page1?__execute_event__=hello&page=2&selectedIds=1,2,3,5');
- const [pushedData, title, url] = b.buildPushStateArgs()
- expect(url).toEqual('/page1?page=2&selectedIds=1,2,3,5');
- expect(pushedData).toEqual({ query: { page: '2', selectedIds: ['1', '2', '3', '5'] }, url: '/page1?page=2&selectedIds=1,2,3,5' });
- });
- it('remove operator will add to current query values', () => {
- const b = plaid().
- eventFunc("hello").
- query("selectedIds", { value: '5', remove: true }).
- mergeQuery(true).
- url("/page1?selectedIds=1,2,3,5&page=2")
- expect(b.buildFetchURL()).toEqual('/page1?__execute_event__=hello&page=2&selectedIds=1,2,3');
- const [pushedData, title, url] = b.buildPushStateArgs()
- expect(url).toEqual('/page1?page=2&selectedIds=1,2,3');
- expect(pushedData).toEqual({ query: { page: '2', selectedIds: ['1', '2', '3'] }, url: '/page1?page=2&selectedIds=1,2,3' });
- });
- it('array with comma', () => {
- const b = plaid().
- eventFunc("hello").
- query("names", ['Hello, Felix', 'How are you']).
- mergeQuery(true).
- url("/page1?selectedIds=1,2,3,5&page=2")
- expect(b.buildFetchURL()).toEqual('/page1?__execute_event__=hello&names=Hello%2C%20Felix,How%20are%20you&page=2&selectedIds=1,2,3,5');
- const [pushedData, title, url] = b.buildPushStateArgs()
- expect(url).toEqual('/page1?names=Hello%2C%20Felix,How%20are%20you&page=2&selectedIds=1,2,3,5');
- expect(pushedData).toEqual({ query: { page: '2', selectedIds: ['1', '2', '3', '5'], names: ['Hello, Felix', 'How are you'] }, url: '/page1?names=Hello%2C%20Felix,How%20are%20you&page=2&selectedIds=1,2,3,5' });
- });
- it('first time add', () => {
- const b = plaid().
- eventFunc("hello").
- query("name", { value: '1', add: true }).
- mergeQuery(true).
- url("/page1")
- expect(b.buildFetchURL()).toEqual('/page1?__execute_event__=hello&name=1');
- const [pushedData, title, url] = b.buildPushStateArgs()
- expect(url).toEqual('/page1?name=1');
- expect(pushedData).toEqual({ query: { name: ['1'] }, url: '/page1?name=1' });
- });
- it('add operator with value array', () => {
- const b = plaid().
- eventFunc("hello").
- query("name", { value: ['1', '2'], add: true }).
- mergeQuery(true).
- url("/page1")
- expect(b.buildFetchURL()).toEqual('/page1?__execute_event__=hello&name=1,2');
- const [pushedData, title, url] = b.buildPushStateArgs()
- expect(url).toEqual('/page1?name=1,2');
- expect(pushedData).toEqual({ query: { name: ['1', '2'] }, url: '/page1?name=1,2' });
- });
- it('remove operator with value array', () => {
- const b = plaid().
- eventFunc("hello").
- query("name", { value: ['1', '2', '5', '8'], remove: true }).
- mergeQuery(true).
- url("/page1?name=1,2,3,4,5,6,7,8,9")
- expect(b.buildFetchURL()).toEqual('/page1?__execute_event__=hello&name=3,4,6,7,9');
- const [pushedData, title, url] = b.buildPushStateArgs()
- expect(url).toEqual('/page1?name=3,4,6,7,9');
- expect(pushedData).toEqual({ query: { name: ['3', '4', '6', '7', '9'] }, url: '/page1?name=3,4,6,7,9' });
- });
- it('remove operator with url and value array', () => {
- const b = plaid().
- eventFunc("hello").
- location({ url: '/page2?name=1,2,3,4,5,6,7,8,9', query: { name: { value: ['1', '2', '5', '8'], remove: true } }, mergeQuery: true }).
- url("/page1?name=1,2,3,4,5,6,7,8,9")
- expect(b.buildFetchURL()).toEqual('/page2?__execute_event__=hello&name=3,4,6,7,9');
- const [pushedData, title, url] = b.buildPushStateArgs()
- expect(url).toEqual('/page2?name=3,4,6,7,9');
- expect(pushedData).toEqual({ query: { name: ['3', '4', '6', '7', '9'] }, url: '/page2?name=3,4,6,7,9' });
- });
- it('with url', () => {
- const b = plaid().
- eventFunc("hello").
- location({ url: '/page2?name=2,3' }).
- url("/page1?name=1,2")
- expect(b.buildFetchURL()).toEqual('/page2?__execute_event__=hello&name=2,3');
- const [pushedData, title, url] = b.buildPushStateArgs()
- expect(url).toEqual('/page2?name=2,3');
- expect(pushedData).toEqual({ query: { name: ['2', '3'] }, url: '/page2?name=2,3' });
- });
- it('location is not set', () => {
- const b = plaid().
- eventFunc("hello").
- url("/page1?name=1,2")
- expect(b.buildFetchURL()).toEqual('/page1?__execute_event__=hello&name=1,2');
- const [pushedData, title, url] = b.buildPushStateArgs()
- expect(url).toEqual('/page1?name=1,2');
- expect(pushedData).toEqual({ query: { name: ['1', '2'] }, url: '/page1?name=1,2' });
- });
- it('without eventFunc', () => {
- const b = plaid().
- url("/page1?name=1,2")
- expect(b.buildFetchURL()).toEqual('/page1?__execute_event__=__reload__&name=1,2');
- const [pushedData, title, url] = b.buildPushStateArgs()
- expect(url).toEqual('/page1?name=1,2');
- expect(pushedData).toEqual({ query: { name: ['1', '2'] }, url: '/page1?name=1,2' });
- });
- });
|