Use RestApiService instead of Polymer element

Change-Id: I67123f91287bb62a30fa1e87ae13335d4b95ffe6
diff --git a/polygerrit-ui/app/elements/diff/gr-apply-fix-dialog/gr-apply-fix-dialog.ts b/polygerrit-ui/app/elements/diff/gr-apply-fix-dialog/gr-apply-fix-dialog.ts
index 66f3b81..44cc258 100644
--- a/polygerrit-ui/app/elements/diff/gr-apply-fix-dialog/gr-apply-fix-dialog.ts
+++ b/polygerrit-ui/app/elements/diff/gr-apply-fix-dialog/gr-apply-fix-dialog.ts
@@ -37,13 +37,12 @@
 } from '../../../types/common';
 import {DiffInfo, DiffPreferencesInfo} from '../../../types/diff';
 import {GrOverlay} from '../../shared/gr-overlay/gr-overlay';
-import {RestApiService} from '../../../services/services/gr-rest-api/gr-rest-api';
 import {isRobot} from '../../../utils/comment-util';
 import {OpenFixPreviewEvent} from '../../../types/events';
+import {appContext} from '../../../services/app-context';
 
 export interface GrApplyFixDialog {
   $: {
-    restAPI: RestApiService & Element;
     applyFixOverlay: GrOverlay;
   };
 }
@@ -101,6 +100,8 @@
 
   private refitOverlay?: () => void;
 
+  private restApiService = appContext.restApiService;
+
   /**
    * Given robot comment CustomEvent object, fetch diffs associated
    * with first robot comment suggested fix and open dialog.
@@ -170,7 +171,7 @@
         new Error('Both _patchNum and changeNum must be set')
       );
     }
-    return this.$.restAPI
+    return this.restApiService
       .getRobotCommentFixPreview(this.changeNum, this._patchNum, fixId)
       .then(res => {
         if (res) {
@@ -290,7 +291,7 @@
       return Promise.reject(new Error('Not all required properties are set.'));
     }
     this._isApplyFixLoading = true;
-    return this.$.restAPI
+    return this.restApiService
       .applyFixSuggestion(changeNum, patchNum, this._currentFix.fix_id)
       .then(res => {
         if (res && res.ok) {
diff --git a/polygerrit-ui/app/elements/diff/gr-apply-fix-dialog/gr-apply-fix-dialog_html.ts b/polygerrit-ui/app/elements/diff/gr-apply-fix-dialog/gr-apply-fix-dialog_html.ts
index 057fd01..52fa9841 100644
--- a/polygerrit-ui/app/elements/diff/gr-apply-fix-dialog/gr-apply-fix-dialog_html.ts
+++ b/polygerrit-ui/app/elements/diff/gr-apply-fix-dialog/gr-apply-fix-dialog_html.ts
@@ -95,5 +95,4 @@
       </div>
     </gr-dialog>
   </gr-overlay>
-  <gr-rest-api-interface id="restAPI"></gr-rest-api-interface>
 `;
diff --git a/polygerrit-ui/app/elements/diff/gr-apply-fix-dialog/gr-apply-fix-dialog_test.js b/polygerrit-ui/app/elements/diff/gr-apply-fix-dialog/gr-apply-fix-dialog_test.js
index cb73885..869b518 100644
--- a/polygerrit-ui/app/elements/diff/gr-apply-fix-dialog/gr-apply-fix-dialog_test.js
+++ b/polygerrit-ui/app/elements/diff/gr-apply-fix-dialog/gr-apply-fix-dialog_test.js
@@ -56,7 +56,7 @@
 
   suite('dialog open', () => {
     setup(() => {
-      sinon.stub(element.$.restAPI, 'getRobotCommentFixPreview')
+      sinon.stub(element.restApiService, 'getRobotCommentFixPreview')
           .returns(Promise.resolve({
             f1: {
               meta_a: {},
@@ -147,7 +147,7 @@
   });
 
   test('next button state updated when suggestions changed', done => {
-    sinon.stub(element.$.restAPI, 'getRobotCommentFixPreview')
+    sinon.stub(element.restApiService, 'getRobotCommentFixPreview')
         .returns(Promise.resolve({}));
     sinon.stub(element.$.applyFixOverlay, 'open').returns(Promise.resolve());
 
@@ -186,13 +186,13 @@
 
   test('apply fix button should call apply ' +
   'and navigate to change view', () => {
-    sinon.stub(element.$.restAPI, 'applyFixSuggestion')
+    sinon.stub(element.restApiService, 'applyFixSuggestion')
         .returns(Promise.resolve({ok: true}));
     sinon.stub(GerritNav, 'navigateToChange');
     element._currentFix = {fix_id: '123'};
 
     return element._handleApplyFix().then(() => {
-      assert.isTrue(element.$.restAPI.applyFixSuggestion
+      assert.isTrue(element.restApiService.applyFixSuggestion
           .calledWithExactly('1', 2, '123'));
       assert.isTrue(GerritNav.navigateToChange.calledWithExactly({
         _number: '1',
@@ -211,13 +211,13 @@
   });
 
   test('should not navigate to change view if incorect reponse', done => {
-    sinon.stub(element.$.restAPI, 'applyFixSuggestion')
+    sinon.stub(element.restApiService, 'applyFixSuggestion')
         .returns(Promise.resolve({}));
     sinon.stub(GerritNav, 'navigateToChange');
     element._currentFix = {fix_id: '123'};
 
     element._handleApplyFix().then(() => {
-      assert.isTrue(element.$.restAPI.applyFixSuggestion
+      assert.isTrue(element.restApiService.applyFixSuggestion
           .calledWithExactly('1', 2, '123'));
       assert.isTrue(GerritNav.navigateToChange.notCalled);
 
@@ -227,7 +227,7 @@
   });
 
   test('select fix forward and back of multiple suggested fixes', done => {
-    sinon.stub(element.$.restAPI, 'getRobotCommentFixPreview')
+    sinon.stub(element.restApiService, 'getRobotCommentFixPreview')
         .returns(Promise.resolve({
           f1: {
             meta_a: {},
@@ -272,7 +272,7 @@
         });
   });
 
-  test('server-error should throw for failed apply call', done => {
+  test('server-error should throw for failed apply call', async () => {
     sinon.stub(window, 'fetch').callsFake((url => {
       if (url.endsWith('/apply')) {
         return Promise.reject(new Error('backend error'));
@@ -287,12 +287,13 @@
     document.addEventListener('network-error', errorStub);
     sinon.stub(GerritNav, 'navigateToChange');
     element._currentFix = {fix_id: '123'};
-    element._handleApplyFix();
-    flush(() => {
-      assert.isFalse(GerritNav.navigateToChange.called);
-      assert.isTrue(errorStub.called);
-      done();
+    let expectedError;
+    await element._handleApplyFix().catch(e => {
+      expectedError = e;
     });
+    assert.isOk(expectedError);
+    assert.isFalse(GerritNav.navigateToChange.called);
+    assert.isTrue(errorStub.called);
   });
 });