Firebase Test

jsFiddle template to run a jasmine test inside jsFiddle. Adds jasmine external resources, and initializes jasmine's HtmlReporter on window load. For more infor check this article: http://opensas.wordpress.com/2013/06/23/sharing-you-javascript-ninja-secrets-run-your-jasmine-tests-on-jsfiddle/

by Alex Cowan

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.2.1/jasmine.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.2.1/jasmine-html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.2.1/boot.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.2.1/jasmine.min.css">
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/8.2.5/firebase-app.js"></script>

<!-- TODO: Add SDKs for Firebase products that you want to use
     https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/8.2.1/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.5/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.5/firebase-analytics.js"></script>

<script>
  // Your web app's Firebase configuration
  // For Firebase JS SDK v7.20.0 and later, measurementId is optional
  var firebaseConfig = {
    apiKey: "AIzaSyCOPgDQ4PAwNtOxZQtkkRpy0LV37VVEzJg",
    authDomain: "hinh-78995.firebaseapp.com",
    projectId: "hinh-78995",
    storageBucket: "hinh-78995.appspot.com",
    messagingSenderId: "389014950808",
    appId: "1:389014950808:web:5599635662f42697b091ed",
    measurementId: "G-XL6BSRZLQE"
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
  firebase.analytics();

</script>

JavaScript

/* WORKING NOTES
:: Is Jasmine set up really OK? verify w/ alt code
Yes- tested w/ current Jasmine tests w/ current code commented out and A-OK.

:: What is returning the permission-denied error? Is the user authenticating?
I'm not seeing it now, but watch out for a permission-denied error in the Console which is probably from too many login attempts, requests, something like that.

:: Can I make the await pattern work? find promising example code/s and get working; if not instrument sequence clearly and add comments in questions
With the async calls in BeforeEach, what I expect to have happen is that login_user and then check_create_review finish and then the test runs. Instead, the test runs then the review error shows then the login runs. 

:: What about a successful review?

::Can I make the brute force timing thing work? try out and then look for sample code; instrument w/ observation and questions
I thought this was a delay you could add, but I don't think I've understood that right based on this: https://jasmine.github.io/api/edge/Clock.html. Maybe this is more the thing to do: https://api.jquery.com/deferred.done/

:: Other/the third thing?



*/

//CODE UNDER TEST


function login_user(email, pwd) {
  console.log("attempting to login user " + email);

  // use the Firebase API to sign in the user
  firebase.auth().signInWithEmailAndPassword(email, pwd).then(function(user) {
    var user = firebase.auth().currentUser;
    console.log("welcoming now..." + user.email + "who has uid " + firebase.auth().currentUser.uid);
  })

}

x = "global init'ed";

function check_create_review(rating1, review1, userid) {
  db = firebase.firestore();
  var userID = {};

  file_path = '/parts/BR2048/reviews'

  console.log("I see rating 1 as " + rating1);

  db.collection(file_path).doc().set({
    rating: rating1,
    review: review1,
    user: userid
  }).catch(error => {
    x = "reviewerror";
    console.log("I am making x " + x);
  })
}

//TESTING...