TypeScript @angular-compiler-testing-src-resource_loader_mock.MockResourceLoader类(方法)实例源码

下面列出了TypeScript @angular-compiler-testing-src-resource_loader_mock.MockResourceLoader 类(方法)源码代码实例,从而了解它的用法。

作者:thorn    项目:angula   
function createSummaries() {
      const resourceLoader = new MockResourceLoader();

      setMetadata(resourceLoader);

      TestBed.configureCompiler({providers: [{provide: ResourceLoader, useValue: resourceLoader}]});
      TestBed.configureTestingModule({imports: [SomeModule], providers: [SomeDep]});

      let summariesPromise = TestBed.compileComponents().then(() => {
        const metadataResolver = TestBed.get(CompileMetadataResolver) as CompileMetadataResolver;
        const summaries = [
          metadataResolver.getNgModuleSummary(SomeModule),
          // test nesting via closures, as we use this in the generated code too.
          () =>
              [metadataResolver.getDirectiveSummary(SomePublicComponent),
               metadataResolver.getDirectiveSummary(SomePrivateComponent),
        ],
          metadataResolver.getDirectiveSummary(SomeDirective),
          metadataResolver.getPipeSummary(SomePipe),
          metadataResolver.getInjectableSummary(SomeService)
        ];
        clearMetadata();
        TestBed.resetTestingModule();
        return () => summaries;
      });

      resourceLoader.flush();
      return summariesPromise;
    }

作者:thorn    项目:angula   
function setMetadata(resourceLoader: MockResourceLoader) {
      Base.parameters = [[SomeDep]];

      SomeModule.annotations = [new NgModule({
        declarations: [SomePublicComponent, SomePrivateComponent, SomeDirective, SomePipe],
        exports: [SomeDirective, SomePipe, SomePublicComponent],
        providers: [SomeService]
      })];

      SomePublicComponent.annotations = [new Component({templateUrl: 'somePublicUrl.html'})];
      resourceLoader.expect('somePublicUrl.html', `Hello public world!`);

      SomePrivateComponent.annotations = [new Component({templateUrl: 'somePrivateUrl.html'})];
      resourceLoader.expect('somePrivateUrl.html', `Hello private world!`);

      SomeDirective.annotations = [new Directive({selector: '[someDir]'})];

      SomePipe.annotations = [new Pipe({name: 'somePipe'})];

      SomeService.annotations = [new Injectable()];
    }

作者:JohnnyQQQ    项目:angula   
describe('MockResourceLoader', () => {
    let resourceLoader: MockResourceLoader;

    beforeEach(() => { resourceLoader = new MockResourceLoader(); });

    function expectResponse(
        request: Promise<string>, url: string, response: string, done: () => void = null) {
      function onResponse(text: string): string {
        if (response === null) {
          throw `Unexpected response ${url} -> ${text}`;
        } else {
          expect(text).toEqual(response);
          if (done != null) done();
        }
        return text;
      }

      function onError(error: string): string {
        if (response !== null) {
          throw `Unexpected error ${url}`;
        } else {
          expect(error).toEqual(`Failed to load ${url}`);
          if (done != null) done();
        }
        return error;
      }

      request.then(onResponse, onError);
    }

    it('should return a response from the definitions',
       inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
         const url = '/foo';
         const response = 'bar';
         resourceLoader.when(url, response);
         expectResponse(resourceLoader.get(url), url, response, () => async.done());
         resourceLoader.flush();
       }));

    it('should return an error from the definitions',
       inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
         const url = '/foo';
         const response: string = null;
         resourceLoader.when(url, response);
         expectResponse(resourceLoader.get(url), url, response, () => async.done());
         resourceLoader.flush();
       }));

    it('should return a response from the expectations',
       inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
         const url = '/foo';
         const response = 'bar';
         resourceLoader.expect(url, response);
         expectResponse(resourceLoader.get(url), url, response, () => async.done());
         resourceLoader.flush();
       }));

    it('should return an error from the expectations',
       inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
         const url = '/foo';
         const response: string = null;
         resourceLoader.expect(url, response);
         expectResponse(resourceLoader.get(url), url, response, () => async.done());
         resourceLoader.flush();
       }));

    it('should not reuse expectations', () => {
      const url = '/foo';
      const response = 'bar';
      resourceLoader.expect(url, response);
      resourceLoader.get(url);
      resourceLoader.get(url);
      expect(() => { resourceLoader.flush(); }).toThrowError('Unexpected request /foo');
    });

    it('should return expectations before definitions',
       inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
         const url = '/foo';
         resourceLoader.when(url, 'when');
         resourceLoader.expect(url, 'expect');
         expectResponse(resourceLoader.get(url), url, 'expect');
         expectResponse(resourceLoader.get(url), url, 'when', () => async.done());
         resourceLoader.flush();
       }));

    it('should throw when there is no definitions or expectations', () => {
      resourceLoader.get('/foo');
      expect(() => { resourceLoader.flush(); }).toThrowError('Unexpected request /foo');
    });

    it('should throw when flush is called without any pending requests', () => {
      expect(() => { resourceLoader.flush(); }).toThrowError('No pending requests to flush');
    });

    it('should throw on unsatisfied expectations', () => {
      resourceLoader.expect('/foo', 'bar');
      resourceLoader.when('/bar', 'foo');
      resourceLoader.get('/bar');
      expect(() => { resourceLoader.flush(); }).toThrowError('Unsatisfied requests: /foo');
    });
//.........这里部分代码省略.........

作者:JohnnyQQQ    项目:angula   
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
   const url = '/foo';
   const response: string = null;
   resourceLoader.expect(url, response);
   expectResponse(resourceLoader.get(url), url, response, () => async.done());
   resourceLoader.flush();
 }));

作者:JohnnyQQQ    项目:angula   
it('should not reuse expectations', () => {
   const url = '/foo';
   const response = 'bar';
   resourceLoader.expect(url, response);
   resourceLoader.get(url);
   resourceLoader.get(url);
   expect(() => { resourceLoader.flush(); }).toThrowError('Unexpected request /foo');
 });

作者:JohnnyQQQ    项目:angula   
expect(() => { resourceLoader.flush(); }).toThrowError('Unexpected request /foo');


问题


面经


文章

微信
公众号

扫码关注公众号