Skip to content

Commit 05d65d0

Browse files
Ismaestroiramos
authored andcommitted
feat(helpers): move helper class to services and add tests
1 parent b4671f5 commit 05d65d0

File tree

11 files changed

+302
-78
lines changed

11 files changed

+302
-78
lines changed

.huskyrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"hooks": {
3+
"pre-push": "npm run ci"
4+
}
5+
}

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"hammerjs": "2.0.8",
7272
"html2canvas": "1.0.0-alpha.12",
7373
"http-server": "0.11.1",
74+
"husky": "1.1.2",
7475
"jasmine-core": "3.2.1",
7576
"jasmine-spec-reporter": "4.2.1",
7677
"karma": "3.0.0",

src/app/app.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {MatSnackBar} from '@angular/material';
66
import {_} from '@biesbjerg/ngx-translate-extract/dist/utils/utils';
77
import {AppConfig} from './configs/app.config';
88
import {LocalStorage} from 'ngx-store';
9-
import {isBrowserValid} from './shared/helpers/utils.helper';
9+
import {UtilsHelperService} from './core/services/utils-helper.service';
1010

1111
declare const require;
1212
declare const Modernizr;
@@ -65,7 +65,7 @@ export class AppComponent implements OnInit {
6565
}
6666

6767
checkBrowser() {
68-
if (isBrowserValid()) {
68+
if (UtilsHelperService.isBrowserValid()) {
6969
this.checkBrowserFeatures();
7070
} else {
7171
this.translateService.get([String(_('changeBrowser'))]).subscribe((texts) => {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {TestBed} from '@angular/core/testing';
2+
import {UtilsHelperService} from './utils-helper.service';
3+
4+
describe('UtilsHelperService', () => {
5+
let utilsHelperService;
6+
beforeEach(() => {
7+
TestBed.configureTestingModule({
8+
providers: [
9+
UtilsHelperService
10+
]
11+
});
12+
13+
utilsHelperService = TestBed.get(UtilsHelperService);
14+
});
15+
16+
it('should create instance', (() => {
17+
expect(utilsHelperService).toBeDefined();
18+
}));
19+
20+
it('should return fadeInOut trigger', (() => {
21+
expect(UtilsHelperService.fadeInOut().name).toBe('fadeInOut');
22+
}));
23+
24+
it('should focus in element', (() => {
25+
const div = document.createElement('div');
26+
expect(UtilsHelperService.scrollToElement(div)).toBe(undefined);
27+
}));
28+
29+
it('should check if is palindrome', (() => {
30+
expect(UtilsHelperService.isPalindrome('')).toBe(true);
31+
expect(UtilsHelperService.isPalindrome('asd')).toBe(false);
32+
expect(UtilsHelperService.isPalindrome('aas')).toBe(false);
33+
expect(UtilsHelperService.isPalindrome('ass')).toBe(false);
34+
expect(UtilsHelperService.isPalindrome('aassaa')).toBe(true);
35+
expect(UtilsHelperService.isPalindrome('asa')).toBe(true);
36+
expect(UtilsHelperService.isPalindrome('asswssa')).toBe(true);
37+
}));
38+
39+
it('should check if browser is valid', (() => {
40+
expect(UtilsHelperService.isBrowserValid()).toBe(true);
41+
}));
42+
});
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import {Injectable} from '@angular/core';
2+
import {animate, AnimationTriggerMetadata, style, transition, trigger} from '@angular/animations';
3+
4+
declare const require;
5+
const bowser = require('bowser');
6+
7+
@Injectable({
8+
providedIn: 'root'
9+
})
10+
export class UtilsHelperService {
11+
static fadeInOut(): AnimationTriggerMetadata {
12+
return trigger('fadeInOut', [
13+
transition(':enter', [
14+
style({opacity: 0}),
15+
animate(500, style({opacity: 1}))
16+
]),
17+
transition(':leave', [
18+
animate(500, style({opacity: 0}))
19+
])
20+
]);
21+
}
22+
23+
static scrollToElement(element) {
24+
if (element) {
25+
const distance = window.pageYOffset - Math.abs(element.getBoundingClientRect().y);
26+
27+
window.scroll({
28+
behavior: 'smooth',
29+
left: 0,
30+
top: element.getBoundingClientRect().top + window.scrollY - 150
31+
});
32+
33+
setTimeout(() => {
34+
element.focus();
35+
element.blur(); // Trigger error messages
36+
element.focus();
37+
}, distance);
38+
}
39+
}
40+
41+
static markFormGroupTouched(formGroup) {
42+
(<any>Object).values(formGroup.controls).forEach(control => {
43+
control.markAsTouched();
44+
45+
if (control.controls) {
46+
UtilsHelperService.markFormGroupTouched(control);
47+
}
48+
});
49+
}
50+
51+
static isPalindrome(str) {
52+
const len = Math.floor(str.length / 2);
53+
for (let i = 0; i < len; i++) {
54+
if (str[i] !== str[str.length - i - 1]) {
55+
return false;
56+
}
57+
}
58+
return true;
59+
}
60+
61+
static isBrowserValid() {
62+
const browser = bowser.getParser(window.navigator.userAgent);
63+
return browser.satisfies({
64+
windows: {
65+
'internet explorer': '>10',
66+
},
67+
macos: {
68+
safari: '>10.1'
69+
},
70+
chrome: '>20.1.1432',
71+
firefox: '>31',
72+
opera: '>22'
73+
});
74+
}
75+
}

src/app/modules/heroes/pages/hero-detail-page/hero-detail-page.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import {HeroService} from '../../shared/hero.service';
44
import {ActivatedRoute, Router} from '@angular/router';
55
import {Location} from '@angular/common';
66
import {AppConfig} from '../../../../configs/app.config';
7-
import {fadeInOut} from '../../../../shared/helpers/utils.helper';
7+
import {UtilsHelperService} from '../../../../core/services/utils-helper.service';
88

99
@Component({
1010
selector: 'app-hero-detail-page',
1111
templateUrl: './hero-detail-page.component.html',
1212
styleUrls: ['./hero-detail-page.component.scss'],
13-
animations: [fadeInOut]
13+
animations: [UtilsHelperService.fadeInOut()]
1414
})
1515

1616
export class HeroDetailPageComponent implements OnInit {

src/app/modules/heroes/pages/heroes-list-page/heroes-list-page.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import {Router} from '@angular/router';
77
import {LoggerService} from '../../../../core/services/logger.service';
88
import {HeroRemoveComponent} from '../../components/hero-remove/hero-remove.component';
99
import {AppConfig} from '../../../../configs/app.config';
10-
import {fadeInOut, isPalindrome} from '../../../../shared/helpers/utils.helper';
1110
import {_} from '@biesbjerg/ngx-translate-extract/dist/utils/utils';
1211
import {TranslateService} from '@ngx-translate/core';
12+
import {UtilsHelperService} from '../../../../core/services/utils-helper.service';
1313

1414
@Component({
1515
selector: 'app-heroes-list-page',
1616
templateUrl: './heroes-list-page.component.html',
1717
styleUrls: ['./heroes-list-page.component.scss'],
18-
animations: [fadeInOut]
18+
animations: [UtilsHelperService.fadeInOut()]
1919
})
2020

2121
export class HeroesListPageComponent implements OnInit {
@@ -95,7 +95,7 @@ export class HeroesListPageComponent implements OnInit {
9595

9696
private onChanges() {
9797
this.newHeroForm.get('name').valueChanges.subscribe((value) => {
98-
if (value && value.length >= 3 && isPalindrome(value)) {
98+
if (value && value.length >= 3 && UtilsHelperService.isPalindrome(value)) {
9999
this.snackBar.open(this.translateService.instant(String(_('yeahPalindrome'))));
100100
} else {
101101
this.snackBar.dismiss();

0 commit comments

Comments
 (0)