summaryrefslogtreecommitdiffstats
path: root/test/utils/Routes.test.js
blob: 12d318c436cdf40c032b2973b5faf2e943647d70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import {
  buildRouteObjWithHash,
  decryptParamsForView,
  buildRouteObjWithFilters,
  changeUrlAddress
} from 'utils/Routes.js';
import {
  encrypt
} from 'utils/Crypto.js';

describe('Routes', () => {
  it('build route with hash', () => {
    const expectedResult = {
      route: '/vnfSearch',
      hashId: 'someCrazyHashHere'
    };

    const result = buildRouteObjWithHash(expectedResult.route, expectedResult.hashId);

    expect(JSON.stringify(result)).toBe(JSON.stringify(expectedResult));
  });

  it('decrypt params for view', () => {
    const stringToEncrypt = 'someCrazyStringHere';
    const encryptedString = encrypt(stringToEncrypt);
    const result = decryptParamsForView(encryptedString);

    expect(JSON.stringify(result)).toBe(JSON.stringify({}));
  });

  it('decrypt params for view with obj', () => {
    const objToEncrypt = [{id: 'someCrazyParamHere'}, {id: 'anotherCrazyParam'}];
    const encryptedObj = encrypt(JSON.stringify(objToEncrypt));
    const result = decryptParamsForView(encryptedObj);

    expect(JSON.stringify(result)).toBe(JSON.stringify(objToEncrypt));
  });

  it('build routes with filters', () => {
    const objToEncrypt = [{id: 'someCrazyParamHere'}, {id: 'anotherCrazyParam'}];
    const encryptedObj = encrypt(JSON.stringify(objToEncrypt));
    const result = decryptParamsForView(encryptedObj);

    expect(JSON.stringify(result)).toBe(JSON.stringify(objToEncrypt));
    const filterObj = {
      filter1: 'value1',
      filter2: undefined,
      filter3: 'anotherValue'
    };
    const routePath = '/vnfSearch';
    const expectedResults = {
      route: routePath,
      filterValues: [
        {
          filterId: 'filter1',
          filterValue: 'value1'
        },
        {
          filterId: 'filter2',
          filterValue: ''
        },
        {
          filterId: 'filter3',
          filterValue: 'anotherValue'
        }
      ]
    }

    const routeWithFilters = buildRouteObjWithFilters(routePath, filterObj);

    expect(JSON.stringify(routeWithFilters)).toBe(JSON.stringify(expectedResults));
  });

  it('change URL address for well known paths', () => {
    const pathObj = {
      route: 'schema',
      filterValues: [
        {
          filterId: 'filter1',
          filterValue: 'value1'
        },
        {
          filterId: 'filter2',
          filterValue: undefined
        },
        {
          filterId: 'filter3',
          filterValue: 'anotherValue'
        }
      ]
    };
    let historyObj = [];
    const filterList = [
      'filter1=value1',
      'filter2=',
      'filter3=anotherValue'
    ];
    const toGo = '/' + pathObj.route + '/' + filterList.toString();
    const expectedResult = [
      toGo,
      {
        lastRoute: pathObj.route
      }
    ];

    changeUrlAddress(pathObj, historyObj);

    expect(JSON.stringify(historyObj)).toBe(JSON.stringify(expectedResult));
  });

  it('change URL address for well known paths with hash id', () => {
    const pathObj = {
      route: 'schema',
      hashId: 'someCrazyHashIdHere'
    };
    let historyObj = [];
    const toGo = '/' + pathObj.route + '/' + pathObj.hashId;
    const expectedResult = [
      toGo,
      {
        lastRoute: pathObj.route
      }
    ];

    changeUrlAddress(pathObj, historyObj);

    expect(JSON.stringify(historyObj)).toBe(JSON.stringify(expectedResult));
  });
})