import { TestBed, waitForAsync, ComponentFixture  } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';

import { of } from 'rxjs';
import { provideMockStore, MockStore } from '@ngrx/store/testing';

import { AppComponent } from './app.component';
import { AppConfigService } from 'src/app/app-config.service';
import * as authActions from 'src/app/auth/auth.actions';

describe('AppComponent', () => {
    let component: AppComponent;
    let fixture: ComponentFixture<AppComponent>;
    let store: MockStore;
    let config: AppConfigService

    beforeEach(waitForAsync(() => {
        TestBed.configureTestingModule({
            imports: [RouterTestingModule],
            declarations: [
                AppComponent
            ],
            providers: [
                provideMockStore({ }),
                AppConfigService
            ]
        }).compileComponents();
        fixture = TestBed.createComponent(AppComponent);
        component = fixture.componentInstance;
        store = TestBed.inject(MockStore);
        config = TestBed.inject(AppConfigService);
    }));

    it('should create the app', () => {
        expect(component).toBeDefined();
    });

    it('authenticationEnabled() should give authentication enabled config key value', () => {
        config.authenticationEnabled = true;
        expect(component.authenticationEnabled()).toBeTruthy();
    });

    it('login() should dispatch login action', () => {
        const spy = jest.spyOn(store, 'dispatch');
        component.login();
        expect(spy).toHaveBeenCalledTimes(1);
        expect(spy).toHaveBeenCalledWith(authActions.login());
    });

    it('logout() should dispatch logout action', () => {
        const spy = jest.spyOn(store, 'dispatch');
        component.logout();
        expect(spy).toHaveBeenCalledTimes(1);
        expect(spy).toHaveBeenCalledWith(authActions.logout());
    });

    it('openEditProfile() should dispatch open edit profile action', () => {
        const spy = jest.spyOn(store, 'dispatch');
        component.openEditProfile();
        expect(spy).toHaveBeenCalledTimes(1);
        expect(spy).toHaveBeenCalledWith(authActions.openEditProfile());
    });

    it('isAnisAdmin() should return observable true if user is authenticated', () => {
        component.userRoles = of(['user']);
        component.isAnisAdmin().subscribe(isAuthenticated => expect(isAuthenticated).toBeFalsy());
        component.userRoles = of(['user', 'anis_admin']);
        component.isAnisAdmin().subscribe(isAuthenticated => expect(isAuthenticated).toBeTruthy());
    });
});