import { isRequestTransaction , getFakeAmount , currentUserLikesTransaction , getQueryWithoutDateFields , getQueryWithoutAmountFields , getQueryWithoutFilterFields , } from "../transactionUtils" ; import faker from "faker" ; import { Transaction , TransactionRequestStatus , DefaultPrivacyLevel , TransactionStatus , TransactionResponseItem , } from "../../models" ; import shortid from "shortid" ; const fakeTransaction = ( requestStatus ? , createdAt ? ) : Transaction => ( { id : shortid ( ) , uuid : faker . random . uuid ( ) , source : shortid ( ) , amount : getFakeAmount ( ) , description : "food" , privacyLevel : DefaultPrivacyLevel . public , receiverId : shortid ( ) , senderId : shortid ( ) , balanceAtCompletion : getFakeAmount ( ) , status : TransactionStatus . pending , requestStatus , requestResolvedAt : faker . date . future ( ) , createdAt : faker . date . past ( ) , modifiedAt : createdAt || faker . date . recent ( ) , } ) ; describe ( "Transaction Utils" , ( ) => { describe ( "isRequestTransaction" , ( ) => { let transaction ; test ( "validates that a transaction is a request" , ( ) => { for ( let s in TransactionRequestStatus ) { transaction = fakeTransaction ( s as TransactionRequestStatus ) ; expect ( isRequestTransaction ( transaction ) ) . toBeTruthy ( ) ; } } ) ; test ( "validates that a transaction is not a request" , ( ) => { transaction = fakeTransaction ( ) ; expect ( isRequestTransaction ( transaction ) ) . toBe ( false ) ; } ) ; test ( "checks if the current user likes a transaction" , ( ) => { const transactionBase = fakeTransaction ( ) ; const currentUser = { id : "9IUK0xpw" , uuid : faker . random . uuid ( ) , firstName : faker . name . firstName ( ) , lastName : faker . name . lastName ( ) , username : faker . internet . userName ( ) , password : "abc123" , email : faker . internet . email ( ) , phoneNumber : faker . phone . phoneNumber ( ) , avatar : faker . internet . avatar ( ) , defaultPrivacyLevel : DefaultPrivacyLevel . public , balance : faker . random . number ( ) , createdAt : faker . date . past ( ) , modifiedAt : faker . date . recent ( ) , } ; const transactionWithLikes = { ... transactionBase , receiverName : "Receiver Name" , receiverAvatar : "/path/to/receiver/avatar.png" , senderAvatar : "/path/to/sender/avatar.png" , senderName : "Sender Name" , likes : [ { id : "ExVksKSH" , uuid : "c849329f-42f7-4ff5-a792-e01c9cec05b5" , userId : "9IUK0xpw" , transactionId : "dKAI-6Ua" , createdAt : new Date ( ) , modifiedAt : new Date ( ) , } , ] , comments : [ ] , } ; expect ( currentUserLikesTransaction ( currentUser , transactionWithLikes ) ) . toBe ( true ) ; const otherCurrentUser = { ... currentUser , id : "ABC123" , } ; expect ( currentUserLikesTransaction ( otherCurrentUser , transactionWithLikes ) ) . toBe ( false ) ; } ) ; } ) ; test ( "gets query without date range fields" , ( ) => { expect ( getQueryWithoutDateFields ( { dateRangeStart : new Date ( ) . toString ( ) , dateRangeEnd : new Date ( ) . toString ( ) , status : TransactionStatus . incomplete , } ) ) . toMatchObject ( { status : "incomplete" } ) ; expect ( getQueryWithoutDateFields ( { status : TransactionStatus . incomplete , } ) ) . toMatchObject ( { status : "incomplete" } ) ; } ) ; test ( "gets query without amount range fields" , ( ) => { expect ( getQueryWithoutAmountFields ( { amountMin : 5 , amountMax : 10 , status : TransactionStatus . incomplete , } ) ) . toMatchObject ( { status : "incomplete" } ) ; expect ( getQueryWithoutAmountFields ( { status : TransactionStatus . incomplete , } ) ) . toMatchObject ( { status : "incomplete" } ) ; } ) ; test ( "gets query without date and amount range fields" , ( ) => { const query = { amountMin : 5 , amountMax : 10 , requestStatus : "pending" , dateRangeStart : "2019-12-01T06:00:00.000Z" , dateRangeEnd : "2019-12-05T06:00:00.000Z" , } ; expect ( getQueryWithoutFilterFields ( query ) ) . toMatchObject ( { requestStatus : "pending" , } ) ; } ) ; } ) ;