/*
 * Copyright (C) 2018 Robert Bosch GmbH Copyright (C) 2018 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

import { FactoryProvider, Injectable } from '@angular/core';
import { fakeAsync, TestBed, waitForAsync } from '@angular/core/testing';
import { CommandbarItemVisibilityService } from './commandbar-item-visibility.service';
import { CommandbarItemComponent } from './commandbar-item.component';
import { BreakpointObserver, LayoutModule, MediaMatcher } from '@angular/cdk/layout';

describe('CommandbarItemVisibilityService', () => {
  let service: CommandbarItemVisibilityService;

  beforeEach(fakeAsync(() => {
    TestBed.configureTestingModule({
      imports: [LayoutModule],
      providers: [{provide: MediaMatcher, useClass: MockMediaMatcher}, <FactoryProvider>{provide: CommandbarItemVisibilityService, useFactory: (breakpointObserver: BreakpointObserver) => new CommandbarItemVisibilityService(breakpointObserver), deps: [BreakpointObserver]} ],
    });

    service = TestBed.inject(CommandbarItemVisibilityService);
  }));

  it('should create', () => {
    expect(service).toBeTruthy();
  });

  // <commandbar-item hide></commandbar-item>
  it(
    'should hide when attribute value empty',
    waitForAsync(() => {
      const sortResult = service.sort([<CommandbarItemComponent>{ hide: '' }]);
      expect(sortResult.visible.length).toEqual(0);
      expect(sortResult.hidden.length).toEqual(1);
    })
  );

  it(
    'should hide when attribute value null',
    waitForAsync(() => {
      const sortResult = service.sort([<CommandbarItemComponent>{ hide: null }]);
      expect(sortResult.visible.length).toEqual(0);
      expect(sortResult.hidden.length).toEqual(1);
    })
  );

  // <commandbar-item></commandbar-item>
  it(
    'should not hide when no hiding defined',
    waitForAsync(() => {
      const sortResult = service.sort([<CommandbarItemComponent>{ hide: undefined }]);
      expect(sortResult.visible.length).toEqual(1);
      expect(sortResult.hidden.length).toEqual(0);
    })
  );

  // <commandbar-item [hide]="false"></commandbar-item>
  it(
    'should not hide when false',
    waitForAsync(() => {
      const sortResult = service.sort([<CommandbarItemComponent>{ hide: false }]);
      expect(sortResult.visible.length).toEqual(1);
      expect(sortResult.hidden.length).toEqual(0);
    })
  );

  // <commandbar-item [hide]="true"></commandbar-item>
  it(
    'should hide when true',
    waitForAsync(() => {
      const sortResult = service.sort([<CommandbarItemComponent>{ hide: true }]);
      expect(sortResult.visible.length).toEqual(0);
      expect(sortResult.hidden.length).toEqual(1);
    })
  );

  /**
   * mock for testing BreakpointObserver
   */
  class MockMediaQueryList {
     constructor(public media: string, public matches: boolean) {}
  }

  /**
   * mock for testing BreakpointObserver
   */
  @Injectable()
  class MockMediaMatcher {
    private _queries = new Map<string, MockMediaQueryList>();

    clear() {
      this._queries.clear();
    }
  }
});
