Skip to content

Commit

Permalink
feat(close): add close method to allow fully closing the 'Coookie P…
Browse files Browse the repository at this point in the history
…olicy' bar

Closes #15
  • Loading branch information
tinesoft committed Jul 3, 2018
1 parent 570bfcf commit 02ada28
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 40 deletions.
78 changes: 38 additions & 40 deletions src/service/cookieconsent.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* tslint:disable:no-unused-variable */
import { NgModule } from '@angular/core';
import { TestBed, async, inject } from '@angular/core/testing';
import { TestBed, fakeAsync, tick} from '@angular/core/testing';
import { NgcCookieConsentModule } from './../cookieconsent.module';
import { NgcCookieConsentService } from './cookieconsent.service';
import { NgcCookieConsentConfig } from './cookieconsent-config';
Expand Down Expand Up @@ -46,6 +45,11 @@ const myConfig: NgcCookieConsentConfig = {

describe('Service: NgcCookieConsent', () => {

afterEach(()=>{
// clean up
CookieConsentUi.clearPage();
});

it('should create the service instance from providers...', () => {
TestBed.configureTestingModule({
providers: [WindowService, NgcCookieConsentService, NgcCookieConsentConfig]
Expand Down Expand Up @@ -76,9 +80,6 @@ describe('Service: NgcCookieConsent', () => {
expect(CookieConsentUi.getCcDismissElement().textContent).toEqual('Got it!');
expect(CookieConsentUi.getCcLinkElement().textContent).toEqual('Learn more');
expect(CookieConsentUi.getCcLinkElement().getAttribute('href')).toEqual('http://cookiesandyou.com');

// clean up
CookieConsentUi.clearPage();
});

it('should inject a custom NgcCookieConsent UI when injecting the service with custom config', () => {
Expand All @@ -93,9 +94,6 @@ describe('Service: NgcCookieConsent', () => {
expect(CookieConsentUi.getCcDismissElement().textContent).toEqual(myConfig.content.dismiss);
expect(CookieConsentUi.getCcLinkElement().textContent).toEqual(myConfig.content.link);
expect(CookieConsentUi.getCcLinkElement().getAttribute('href')).toEqual(myConfig.content.href);

// clean up
CookieConsentUi.clearPage();
});

it('should not inject the NgcCookieConsent UI when not injecting the service', () => {
Expand All @@ -114,9 +112,6 @@ describe('Service: NgcCookieConsent', () => {

expect(service.getStatus()).not.toBeNull();
expect(service.getStatus()).toEqual({ deny: 'deny', allow: 'allow', dismiss: 'dismiss' });

// clean up
CookieConsentUi.clearPage();
});

it('should return default transition when calling getTransition()', () => {
Expand All @@ -126,9 +121,6 @@ describe('Service: NgcCookieConsent', () => {
let service = TestBed.get(NgcCookieConsentService); // inject the service from root injector

expect(service.getTransition()).toBe(true);

// clean up
CookieConsentUi.clearPage();
});


Expand All @@ -145,9 +137,6 @@ describe('Service: NgcCookieConsent', () => {
expect(typeof (config.onInitialize)).toEqual('function');
expect(typeof (config.onStatusChange)).toEqual('function');
expect(typeof (config.onRevokeChoice)).toEqual('function');

// clean up
CookieConsentUi.clearPage();
});

it('should emit onPopupOpen$ event when calling onPopupOpen() callback', () => {
Expand All @@ -165,9 +154,6 @@ describe('Service: NgcCookieConsent', () => {
config.onPopupOpen();

expect(calls).toEqual(2);

// clean up
CookieConsentUi.clearPage();
});


Expand All @@ -186,9 +172,6 @@ describe('Service: NgcCookieConsent', () => {
config.onPopupClose();

expect(calls).toEqual(2);

// clean up
CookieConsentUi.clearPage();
});


Expand All @@ -210,9 +193,6 @@ describe('Service: NgcCookieConsent', () => {
config.onInitialize('status2');

expect(calls).toEqual(2);

// clean up
CookieConsentUi.clearPage();
});

it('should emit statusChange$ event when calling onStatusChange() callback', () => {
Expand All @@ -233,9 +213,6 @@ describe('Service: NgcCookieConsent', () => {
config.onStatusChange('status2', 'chosenBefore2');

expect(calls).toEqual(2);

// clean up
CookieConsentUi.clearPage();
});

it('should emit revokeChoice$ event when calling onRevokeChoice() callback', () => {
Expand All @@ -253,9 +230,6 @@ describe('Service: NgcCookieConsent', () => {
config.onRevokeChoice();

expect(calls).toEqual(2);

// clean up
CookieConsentUi.clearPage();
});

it('should destroy the NgcCookieConsent UI when calling "destroy"', () => {
Expand All @@ -270,6 +244,26 @@ describe('Service: NgcCookieConsent', () => {
expect(CookieConsentUi.getCcElement()).toBeNull();
});

it('should toggle RevokeButton the NgcCookieConsent UI when clicking "deny"', fakeAsync(() => {
TestBed.configureTestingModule({
imports: [NgcCookieConsentModule.forRoot(myConfig)]
});
let service = TestBed.get(NgcCookieConsentService); // inject the service from root injector
expect(CookieConsentUi.getCcElement()).not.toBeNull();

let revokeBtn:HTMLElement = CookieConsentUi.getCcRevokeElement();
expect(revokeBtn.style.display).toEqual("none");

tick(100);// let the bar time to appear and then simulate click on "Decline" button
let denyBtn:HTMLElement = CookieConsentUi.getCcDenyElement();
denyBtn.click();

revokeBtn = CookieConsentUi.getCcRevokeElement();
expect(revokeBtn.style.display).toEqual("");

expect(CookieConsentUi.getCcElement()).not.toBeNull();
}));

it('should fade in the NgcCookieConsent UI when calling "fadeIn"', () => {
TestBed.configureTestingModule({
imports: [NgcCookieConsentModule.forRoot(minimumConfig)]
Expand Down Expand Up @@ -317,20 +311,24 @@ export class CookieConsentUi {
return document.querySelectorAll('div.cc-window.cc-banner');
}

public static getCcMessageElement(): Element {
return document.querySelectorAll('span.cc-message').item(0);
public static getCcMessageElement(): HTMLElement {
return document.querySelectorAll('span.cc-message').item(0) as HTMLElement;
}

public static getCcLinkElement(): HTMLElement {
return document.querySelectorAll('a.cc-link').item(0) as HTMLElement;
}

public static getCcLinkElement(): Element {
return document.querySelectorAll('a.cc-link').item(0);
public static getCcDenyElement(): HTMLElement {
return document.querySelectorAll('a.cc-btn.cc-deny').item(0) as HTMLElement;
}

public static getCcDenyElement(): Element {
return document.querySelectorAll('a.cc-btn.cc-deny').item(0);
public static getCcDismissElement(): HTMLElement {
return document.querySelectorAll('a.cc-btn.cc-dismiss').item(0) as HTMLElement;
}

public static getCcDismissElement(): Element {
return document.querySelectorAll('a.cc-btn.cc-dismiss').item(0);
public static getCcRevokeElement(): HTMLElement {
return document.querySelectorAll('div.cc-revoke').item(0) as HTMLElement;
}

constructor() {
Expand Down
6 changes: 6 additions & 0 deletions src/service/cookieconsent.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface NgcCookieConsentPopup {
getStatus(): NgcCookieConsentStatus;
clearStatus(): void;
open(): NgcCookieConsentPopup;
close(showRevoke: boolean): NgcCookieConsentPopup;
destroy(): void;
fadeIn(): void;
fadeOut(): void;
Expand Down Expand Up @@ -184,6 +185,11 @@ export class NgcCookieConsentService {
return this.popupInstance.open();
}

close(showRevoke: boolean): NgcCookieConsentPopup {
this.checkPopupInstantiated('close');
return this.popupInstance.close(showRevoke);
}

destroy(): void {
this.checkPopupInstantiated('destroy');
this.popupInstance.destroy();
Expand Down

0 comments on commit 02ada28

Please sign in to comment.