8
0
Эх сурвалжийг харах

Improved the manual test.

Maciej Bukowski 7 жил өмнө
parent
commit
79dc48db47

+ 3 - 5
packages/ckeditor5-cloud-services/tests/manual/token.html

@@ -2,10 +2,8 @@
 	<h2>Token URL callback example</h2>
 </div>
 
-<button id="refresh-token">Refresh token</button>
-
 <style>
-	#output pre {
+	pre {
 		white-space: pre-wrap;
 		white-space: -moz-pre-wrap;
 		white-space: -pre-wrap;
@@ -14,6 +12,6 @@
 	}
 </style>
 
-<div id="output">
+<div id="request"></div>
 
-</div>
+<pre id="output"></pre>

+ 28 - 25
packages/ckeditor5-cloud-services/tests/manual/token.js

@@ -11,11 +11,12 @@ import CloudServices from '../../src/cloudservices';
 
 import { TOKEN_URL, UPLOAD_URL } from '../_utils/cloud-services-config';
 
-const output = document.querySelector( '#output' );
-const refreshTokenButton = document.querySelector( '#refresh-token' );
+const output = document.getElementById( 'output' );
+const refreshTokenButton = document.getElementById( 'refresh-token' );
+const requestOutput = document.getElementById( 'request' );
 
 ClassicEditor
-	.create( document.querySelector( '#editor' ), {
+	.create( document.getElementById( 'editor' ), {
 		cloudServices: {
 			tokenUrl: getToken,
 			uploadUrl: UPLOAD_URL
@@ -25,19 +26,35 @@ ClassicEditor
 	} )
 	.then( editor => {
 		window.editor = editor;
-
-		const cloudServices = editor.plugins.get( CloudServices );
-
-		refreshTokenButton.addEventListener( 'click', () => {
-			cloudServices.token._refreshToken();
-		} );
 	} )
 	.catch( err => {
+		output.innerText = err.stack;
 		console.error( err.stack );
 	} );
 
 window.addEventListener( refreshTokenButton, () => {} );
 
+function handleRequest( xhr, resolve, reject ) {
+	requestOutput.innerHTML = `
+		<div>XHR request: <pre class='xhr-data'></pre></div>
+		<button class="resolve">Resolve with the xhr response</button>
+		<button class="reject">Reject with an error</button>
+	`;
+
+	const xhrSpan = requestOutput.querySelector( '.xhr-data' );
+	const xhrData = {
+		status: xhr.status,
+		response: xhr.response,
+	};
+	xhrSpan.innerText = JSON.stringify( xhrData, null, 2 );
+
+	const resolveButton = requestOutput.querySelector( '.resolve' );
+	resolveButton.addEventListener( 'click', () => resolve( xhr.response ) );
+
+	const rejectButton = requestOutput.querySelector( '.reject' );
+	rejectButton.addEventListener( 'click', () => reject( new Error( 'Cannot download new token!' ) ) );
+}
+
 function getToken() {
 	return new Promise( ( resolve, reject ) => {
 		const xhr = new XMLHttpRequest();
@@ -45,14 +62,7 @@ function getToken() {
 		xhr.open( 'GET', TOKEN_URL );
 
 		xhr.addEventListener( 'load', () => {
-			const statusCode = xhr.status;
-			const xhrResponse = xhr.response;
-
-			if ( statusCode < 200 || statusCode > 299 ) {
-				return reject( new Error( 'Cannot download new token!' ) );
-			}
-
-			return resolve( xhrResponse );
+			handleRequest( xhr, resolve, reject );
 		} );
 
 		xhr.addEventListener( 'error', () => reject( new Error( 'Network Error' ) ) );
@@ -60,15 +70,8 @@ function getToken() {
 
 		xhr.send();
 	} ).then( response => {
-		log( 'Token: ' + response );
+		output.innerText = `Response: ${ response }`;
 
 		return response;
 	} );
 }
-
-function log( message ) {
-	const pre = document.createElement( 'pre' );
-	pre.innerText = message;
-
-	output.appendChild( pre );
-}

+ 21 - 3
packages/ckeditor5-cloud-services/tests/manual/token.md

@@ -1,4 +1,22 @@
-### Token sample
+## Token sample
 
-1. After editor initialization there should be visible one token that was requested
-1. After clicking the `Refresh token` button there should be another token in the output.
+### Scenario 1
+
+**Actions**
+1. Click `Resolve` button to resolve the pending promise with the XHR response.
+
+**Expected result**
+* Editor isn't initialized at the start of the scenario.
+* After clicking the button the editor initializes with no error in the console.
+* The token used in the editor should be visible in the output.
+
+### Scenario 2
+
+**Actions**
+1. Restart the page with <kbd>CMD</kbd> + <kbd>R</kbd>
+1. Click `Reject` button to reject the pending promise with an error.
+
+**Expected result**
+* Editor isn't initialized at the start of the scenario.
+* The token is different than the previous token.
+* After clicking the button the editor doesn't initialize and there is an error in the output - `Error: Cannot download new token!`.