Advertisement
Guest User

tests

a guest
Aug 31st, 2018
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Chai is a commonly used library for creating unit test suites. It is easily extended with plugins.
  2. import { assert } from 'chai';
  3.  
  4. // Sinon is a library used for mocking or verifying function calls in JavaScript.
  5. import * as sinon from 'sinon';
  6.  
  7. // Require firebase-admin so we can stub out some of its methods.
  8. import * as admin from 'firebase-admin';
  9.  
  10. // Require and initialize firebase-functions-test. Since we are not passing in any parameters, it will
  11. // be initialized in an "offline mode", which means we have to stub out all the methods that interact
  12. // with Firebase services.
  13. import * as _test from 'firebase-functions-test';
  14.  
  15. const firebaseTest = _test();
  16.  
  17. describe('Cloud Functions', () => {
  18.   let adminInitStub;
  19.  
  20.   // @ts-ignore
  21.   before(() => {
  22.     // [START stubAdminInit]
  23.     // If index.js calls admin.initializeApp at the top of the file,
  24.     // we need to stub it out before requiring index.js. This is because the
  25.     // functions will be executed as a part of the require process.
  26.     // Here we stub admin.initializeApp to be a dummy function that doesn't do anything.
  27.     adminInitStub = sinon.stub(admin, 'initializeApp');
  28.     // [END stubAdminInit]
  29.   });
  30.  
  31.   // @ts-ignore
  32.   after(() => {
  33.     // Restore admin.initializeApp() to its original method.
  34.     adminInitStub.restore();
  35.  
  36.     // Do other cleanup tasks.
  37.     firebaseTest.cleanup();
  38.  
  39.     // Cleaning up env
  40.     process.env.FUNCTION_NAME = undefined;
  41.   });
  42.  
  43.   describe('signUp', () => {
  44.     process.env.FUNCTION_NAME = 'signUp';
  45.  
  46.     it('it should write all the default data', async () => {
  47.  
  48.       const firebaseObject = {
  49.         firestore: () => ({
  50.           collection: () => ({
  51.             doc: () => {}
  52.           }),
  53.           batch: () => ({
  54.             set: () => {},
  55.             commit: () => Promise.resolve('ok')
  56.           })
  57.         })
  58.       };
  59.      
  60.       adminInitStub.callsFake(() => firebaseObject);
  61.  
  62.       const userSignup = require('../src/userSignup');
  63.       const user = {
  64.         id: 'testId'
  65.       };
  66.      
  67.       const wrapped = firebaseTest.wrap(userSignup);
  68.  
  69.       assert.equal(await wrapped(user), 'ok');
  70.     });
  71.  
  72.     it('it should NOT write all the default data', async () => {
  73.      
  74.       const firebaseObject = {
  75.         firestore: () => ({
  76.           collection: () => ({
  77.             doc: () => {}
  78.           }),
  79.           batch: () => ({
  80.             set: () => {},
  81.             commit: () => Promise.reject('error')
  82.           })
  83.         })
  84.       };
  85.  
  86.       adminInitStub.callsFake(() => firebaseObject);
  87.  
  88.       const userSignup = require('../src/userSignup');
  89.       const user = {
  90.         id: 'testId'
  91.       };
  92.      
  93.       const wrapped = firebaseTest.wrap(userSignup);
  94.      
  95.       assert.equal(await wrapped(user), 'error');
  96.     });
  97.   });
  98. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement