Skip to content

Instantly share code, notes, and snippets.

View akshitzaveri's full-sized avatar

Akshit Zaveri akshitzaveri

View GitHub Profile
@akshitzaveri
akshitzaveri / Firestore.swift
Last active April 23, 2020 17:28
Setup Firebase Firestore connecting to localhost
// Checking if unit tests are running
if ProcessInfo.processInfo.environment["unit_tests"] == "true" {
print("Setting up Firebase emulator localhost:8080")
let settings = Firestore.firestore().settings
settings.host = "localhost:8080"
settings.isPersistenceEnabled = false
settings.isSSLEnabled = false
Firestore.firestore().settings = settings
}
init?(from document: [String: Any]) {
guard let idString = document["id"] as? String,
let id = Int(idString),
let name = document["name"] as? String,
let priceString = document["pricePerUnit"] as? String,
let price = Double(priceString) else { return nil }
self.init(id: id, name: name, pricePerUnit: price)
}
typealias GetProductsCompletionHandler = ([Product]) -> Void
func getProductsFromCart(_ completion: @escaping GetProductsCompletionHandler) {
self.db.collection("cart").getDocuments { (snapshot, error) in
// Handle error
if error != nil {
completion([])
return
}
func test_WhenProductIsNotNilAndAddToCartIsCalled_ThenTheProductIsAdded() {
// given
self.viewModel.product = Product(id: 1, name: "Grey T-Shirt", pricePerUnit: 20)
let exp = self.expectation(description: "Waiting for async operation")
// when
self.viewModel.addToCart { (result) in
// then
switch result {
case .success:
import XCTest
import Firebase
extension XCTestCase {
func clearFirestore() {
let semaphore = DispatchSemaphore(value: 0)
let projectId = FirebaseApp.app()!.options.projectID!
let url = URL(string: "http://localhost:8080/emulator/v1/projects/\(projectId)/databases/(default)/documents")!
var request = URLRequest(url: url)
override func tearDownWithError() throws {
self.viewModel = nil
self.clearFirestore()
}
self.stackView.setCustomSpacing(32, after: self.secondLabel)
import Foundation
protocol Animal {
func walk()
func run()
}
class Elephant: Animal {
func walk() {
print("I am walking")
import Foundation
@objc protocol Animal {
func walk()
@objc optional func run()
}
class Elephant: NSObject, Animal {
func walk() {
print("I am walking")
import Foundation
protocol Animal {
func walk()
func run()
}
extension Animal {
func run() {
}