All files service.ts

96.15% Statements 25/26
100% Branches 8/8
90% Functions 9/10
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 983x 3x                                         3x 18x         18x             18x 18x       201x       193x       128x       32x       32x       29x                   20x 4x     16x   16x 16x         12x 1x     11x   11x 11x     11x 12x        
import { makeObservable, observable, action } from 'mobx'
import { CookiesManager, CookiesManagerWrapper } from './cookies-manager'
 
export interface ServiceDefinition {
    id: string
    needConsent: boolean
    type?: string
    name?: string
    cookies: string[]
}
 
export interface ServiceOptions extends ServiceDefinition {
    onAccept?: (manager: CookiesManagerWrapper) => void
    onDecline?: () => void
}
 
export interface ServiceInformations extends ServiceDefinition {
    consent: ConsentResponse
}
 
export type ConsentResponse = 'yes' | 'no' | 'unknown'
 
export class Service implements ServiceInformations {
    public consent: ConsentResponse = 'unknown'
    protected _options: ServiceOptions
    protected _cookies: CookiesManager
 
    constructor(options: ServiceOptions, cookies: CookiesManager) {
        makeObservable(this, {
            consent: observable,
 
            accept: action,
            decline: action,
        })
 
        this._options = options
        this._cookies = cookies
    }
 
    public get id (): string {
        return this._options.id
    }
 
    public get needConsent (): boolean {
        return this._options.needConsent
    }
 
    public get type (): string {
        return this._options.type || 'default'
    }
 
    public get name (): string {
        return this._options.name !== undefined ? this._options.name : this.type+'.'+this.id
    }
 
    public get cookies (): string[] {
        return this._options.cookies
    }
 
    public get definition (): ServiceDefinition {
        return {
            id: this.id,
            needConsent: this.needConsent,
            type: this.type,
            name: this.name,
            cookies: this.cookies
        }
    }
 
    public accept (): void {
        if (this.consent == 'yes') {
            return
        }
 
        this.consent = 'yes'
 
        if (this._options.onAccept) {
            this._options.onAccept(new CookiesManagerWrapper(this._cookies, this.definition.cookies, () => this.consent === 'yes'))
        }
    }
 
    public decline (): void {
        if (this.consent == 'no') {
            return
        }
 
        this.consent = 'no'
 
        if (this._options.onDecline) {
            this._options.onDecline()
        }
 
        for (const cookie of this.definition.cookies) {
            this._cookies.remove(cookie)
        }
    }
}