Skip to content

Commit 8ebdaf9

Browse files
committed
chore(refactor): add unit tests
1 parent 086fafc commit 8ebdaf9

10 files changed

Lines changed: 2978 additions & 84 deletions

File tree

.github/workflows/build.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Build
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
15+
- name: Setup Node.js
16+
uses: actions/setup-node@v4
17+
with:
18+
node-version-file: '.nvmrc'
19+
cache: 'yarn'
20+
21+
- name: Install dependencies
22+
run: yarn install --frozen-lockfile
23+
24+
- name: Build widget
25+
run: make

.github/workflows/tests.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
15+
- name: Setup Node.js
16+
uses: actions/setup-node@v4
17+
with:
18+
node-version-file: '.nvmrc'
19+
cache: 'yarn'
20+
21+
- name: Install dependencies
22+
run: yarn install --frozen-lockfile
23+
24+
- name: Run tests
25+
run: yarn test

app/app.js

Lines changed: 5 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
define(['jquery', 'ractive', 'rv!templates/template', 'text!css/widget-styles.css'], function ($, Ractive, mainTemplate, css) {
1+
define(['jquery', 'ractive', 'rv!templates/template', 'text!css/widget-styles.css', 'app/helpers'], function ($, Ractive, mainTemplate, css, helpers) {
22

33
'use strict';
44

55
$.noConflict();
6-
var MAX_DETAIL_LEN = 100;
76
var xhr_suggestions = null;
87
var timeout_suggestions = null;
98
var search_widget = {
@@ -89,7 +88,7 @@ define(['jquery', 'ractive', 'rv!templates/template', 'text!css/widget-styles.cs
8988
ev.original.preventDefault();
9089
if (newPage > totalPages || newPage < 1) return false;
9190

92-
changePagination(that, newPage);
91+
helpers.changePagination(that, newPage);
9392
doSearch(that, false);
9493
}
9594
});
@@ -110,17 +109,12 @@ define(['jquery', 'ractive', 'rv!templates/template', 'text!css/widget-styles.cs
110109
url: url + '/'+term+'?page='+page+'&page_size='+perPage,
111110
dataType: "json"
112111
}).done(function(resp) {
113-
var results = resp.results.map(function(r) {
114-
var detail = r.hasOwnProperty('meta_description') ? r.meta_description : r.content;
115-
detail = detail.length > MAX_DETAIL_LEN ? detail.substring(0, MAX_DETAIL_LEN) + '...' : detail;
116-
var title = r.hasOwnProperty('meta_title') ? r.meta_title : r.title;
117-
return {link: r.url, title: title, detail: detail};
118-
});
112+
var results = helpers.mapSearchResults(resp.results);
119113

120114
that.set('total', resp.qty);
121115
that.set('results', results);
122116

123-
if(reset) resetPagination(that);
117+
if(reset) helpers.resetPagination(that);
124118

125119
}).fail(function(resp) {
126120
// error response
@@ -135,9 +129,7 @@ define(['jquery', 'ractive', 'rv!templates/template', 'text!css/widget-styles.cs
135129
url: 'https://'+that.baseUrl+'/api/public/v1/suggestions/'+that.context+'/'+term,
136130
dataType: "json"
137131
}).done(function(resp) {
138-
var results = resp.results.map(function(r) {
139-
return {link: r.payload, title: r.term};
140-
});
132+
var results = helpers.mapSuggestions(resp.results);
141133
xhr_suggestions = null;
142134
that.set('suggestions', results);
143135

@@ -146,64 +138,6 @@ define(['jquery', 'ractive', 'rv!templates/template', 'text!css/widget-styles.cs
146138
});
147139
}
148140

149-
function resetPagination(that) {
150-
var total = that.get('total');
151-
var perPage = that.get('perPage');
152-
var totalPages = Math.ceil(total / perPage);
153-
var lastPage = (totalPages < 5) ? totalPages : 5;
154-
var newPagesToShow = [];
155-
156-
for( var i = 1; i <= lastPage; i++) {
157-
newPagesToShow.push(i);
158-
}
159-
160-
that.set('page', 1);
161-
that.set('pagesToShow', newPagesToShow);
162-
163-
// change results
164-
var newResultLimit = (perPage);
165-
newResultLimit = (newResultLimit > total) ? total : newResultLimit;
166-
that.set('fromResult', 1);
167-
that.set('toResult', newResultLimit);
168-
}
169-
170-
function changePagination(that, newPage) {
171-
var total = that.get('total');
172-
var pagesToShow = that.get('pagesToShow');
173-
var perPage = that.get('perPage');
174-
var lastPage = pagesToShow[pagesToShow.length - 1];
175-
var firstPage = pagesToShow[0];
176-
var totalPages = Math.ceil(total / perPage);
177-
var newPagesToShow = [];
178-
179-
that.set('page', newPage);
180-
181-
// change results
182-
var newResultLimit = (newPage * perPage);
183-
newResultLimit = (newResultLimit > total) ? total : newResultLimit;
184-
that.set('fromResult', ((newPage - 1) * perPage) + 1);
185-
that.set('toResult', newResultLimit);
186-
187-
// change pagination
188-
if (newPage > lastPage - 1 || newPage < firstPage + 1) {
189-
var pageFrom;
190-
var pageTo = ((newPage + 2) < 5) ? 5 : (newPage + 2);
191-
newPagesToShow = []
192-
193-
if (pageTo > totalPages) {
194-
pageTo = totalPages;
195-
}
196-
197-
pageFrom = ((pageTo - 4) < 1) ? 1 : (pageTo - 4);
198-
199-
for( var i = pageFrom; i <= pageTo; i++) {
200-
newPagesToShow.push(i);
201-
}
202-
203-
that.set('pagesToShow', newPagesToShow);
204-
}
205-
}
206-
207141
function centerPopup(el) {
208142
var winW = $(window).width();
209143
var newPopWidth = winW * 0.8;

app/helpers.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
define([], function () {
2+
3+
'use strict';
4+
5+
var MAX_DETAIL_LEN = 100;
6+
7+
function mapSearchResults(results) {
8+
return results.map(function(r) {
9+
var detail = r.hasOwnProperty('meta_description') ? r.meta_description : r.content;
10+
detail = detail.length > MAX_DETAIL_LEN ? detail.substring(0, MAX_DETAIL_LEN) + '...' : detail;
11+
var title = r.hasOwnProperty('meta_title') ? r.meta_title : r.title;
12+
return {link: r.url, title: title, detail: detail};
13+
});
14+
}
15+
16+
function mapSuggestions(results) {
17+
return results.map(function(r) {
18+
return {link: r.payload, title: r.term};
19+
});
20+
}
21+
22+
function resetPagination(that) {
23+
var total = that.get('total');
24+
var perPage = that.get('perPage');
25+
var totalPages = Math.ceil(total / perPage);
26+
var lastPage = (totalPages < 5) ? totalPages : 5;
27+
var newPagesToShow = [];
28+
29+
for (var i = 1; i <= lastPage; i++) {
30+
newPagesToShow.push(i);
31+
}
32+
33+
that.set('page', 1);
34+
that.set('pagesToShow', newPagesToShow);
35+
36+
var newResultLimit = perPage;
37+
newResultLimit = (newResultLimit > total) ? total : newResultLimit;
38+
that.set('fromResult', 1);
39+
that.set('toResult', newResultLimit);
40+
}
41+
42+
function changePagination(that, newPage) {
43+
var total = that.get('total');
44+
var pagesToShow = that.get('pagesToShow');
45+
var perPage = that.get('perPage');
46+
var lastPage = pagesToShow[pagesToShow.length - 1];
47+
var firstPage = pagesToShow[0];
48+
var totalPages = Math.ceil(total / perPage);
49+
var newPagesToShow = [];
50+
51+
that.set('page', newPage);
52+
53+
var newResultLimit = (newPage * perPage);
54+
newResultLimit = (newResultLimit > total) ? total : newResultLimit;
55+
that.set('fromResult', ((newPage - 1) * perPage) + 1);
56+
that.set('toResult', newResultLimit);
57+
58+
if (newPage > lastPage - 1 || newPage < firstPage + 1) {
59+
var pageFrom;
60+
var pageTo = ((newPage + 2) < 5) ? 5 : (newPage + 2);
61+
62+
if (pageTo > totalPages) {
63+
pageTo = totalPages;
64+
}
65+
66+
pageFrom = ((pageTo - 4) < 1) ? 1 : (pageTo - 4);
67+
68+
for (var i = pageFrom; i <= pageTo; i++) {
69+
newPagesToShow.push(i);
70+
}
71+
72+
that.set('pagesToShow', newPagesToShow);
73+
}
74+
}
75+
76+
return {
77+
MAX_DETAIL_LEN: MAX_DETAIL_LEN,
78+
mapSearchResults: mapSearchResults,
79+
mapSuggestions: mapSuggestions,
80+
resetPagination: resetPagination,
81+
changePagination: changePagination
82+
};
83+
84+
});

babel.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
plugins: ['transform-amd-to-commonjs']
3+
};

embed.min.js

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jest.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
testEnvironment: 'jsdom',
3+
testMatch: ['<rootDir>/tests/**/*.test.js'],
4+
transform: {
5+
'\\.js$': 'babel-jest'
6+
},
7+
transformIgnorePatterns: [
8+
'/node_modules/'
9+
]
10+
};

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
"rv": "^0.1.8",
1616
"rvc": "^0.5.0"
1717
},
18-
"devDependencies": {},
18+
"devDependencies": {
19+
"@babel/core": "^7.29.0",
20+
"babel-jest": "^30.3.0",
21+
"babel-plugin-transform-amd-to-commonjs": "^1.6.0",
22+
"jest": "^30.3.0",
23+
"jest-environment-jsdom": "^30.3.0"
24+
},
1925
"scripts": {
20-
"test": "echo \"Error: no test specified\" && exit 1"
26+
"test": "jest"
2127
},
2228
"repository": {
2329
"type": "git",

0 commit comments

Comments
 (0)