Преглед на файлове

Added `Token#dstroy` method.

Maciej Bukowski преди 7 години
родител
ревизия
67c1046798
променени са 2 файла, в които са добавени 30 реда и са изтрити 0 реда
  1. 7 0
      packages/ckeditor-cloud-services-core/src/token/token.js
  2. 23 0
      packages/ckeditor-cloud-services-core/tests/token/token.js

+ 7 - 0
packages/ckeditor-cloud-services-core/src/token/token.js

@@ -101,6 +101,13 @@ class Token {
 	}
 
 	/**
+	 * Destroys token instance. Stops refreshing.
+	 */
+	destroy() {
+		this._stopRefreshing();
+	}
+
+	/**
 	 * Starts value refreshing every `refreshInterval` time.
 	 *
 	 * @protected

+ 23 - 0
packages/ckeditor-cloud-services-core/tests/token/token.js

@@ -104,6 +104,29 @@ describe( 'Token', () => {
 		} );
 	} );
 
+	describe( 'destroy', () => {
+		it( 'should stop refreshing the token', () => {
+			const clock = sinon.useFakeTimers( { toFake: [ 'setInterval', 'clearInterval' ] } );
+			const token = new Token( 'http://token-endpoint', { initValue: 'initValue' } );
+
+			return token.init()
+				.then( () => {
+					clock.tick( 3600000 );
+					clock.tick( 3600000 );
+
+					expect( requests.length ).to.equal( 2 );
+
+					token.destroy();
+
+					clock.tick( 3600000 );
+					clock.tick( 3600000 );
+					clock.tick( 3600000 );
+
+					expect( requests.length ).to.equal( 2 );
+				} );
+		} );
+	} );
+
 	describe( '_refreshToken()', () => {
 		it( 'should get a token from the specified address', done => {
 			const token = new Token( 'http://token-endpoint', { initValue: 'initValue', autoRefresh: false } );