import { array , Decoder , fields , multi , record , string } from "../" ; test ( "recursive data structure" , ( ) => { type Person = { name : string ; friends : Array < Person > ; } ; const personDecoder1 = fields ( ( field ) : Person => ( { name : field ( "name" , string ) , friends : field ( "friends" , array ( personDecoder1 ) ) , } ) ) ; const data = { name : "John" , friends : [ { name : "Alice" , friends : [ ] , } , { name : "Bob" , friends : [ { name : "Charlie" , friends : [ ] , } , ] , } , ] , } ; expect ( personDecoder1 ( data ) ) . toMatchInlineSnapshot ( `
    Object {
      "friends": Array [
        Object {
          "friends": Array [],
          "name": "Alice",
        },
        Object {
          "friends": Array [
            Object {
              "friends": Array [],
              "name": "Charlie",
            },
          ],
          "name": "Bob",
        },
      ],
      "name": "John",
    }
  ` ) ; } ) ; test ( "recurse non-record" , ( ) => { type Dict = { [ key ] : Dict | number ; } ; const dictDecoder = record ( multi ( { number : ( value ) => value , object : ( value ) => dictDecoder ( value ) , } ) ) ; const data = { t : { i : { n : { y : 1 , t : 2 , } , e : 3 , } , } , } ; expect ( dictDecoder ( data ) ) . toMatchInlineSnapshot ( `
    Object {
      "t": Object {
        "i": Object {
          "e": 3,
          "n": Object {
            "t": 2,
            "y": 1,
          },
        },
      },
    }
  ` ) ; } ) ; test ( "circular objects" , ( ) => { type Person = { name : string ; likes : Person ; } ; const personDecoder = fields ( ( field ) : Person => ( { name : field ( "name" , string ) , likes : field ( "likes" , personDecoder ) , } ) ) ; const alice = { name : "Alice" , likes : undefined , } ; const bob = { name : "Bob" , likes : alice , } ; alice . likes = bob ; const wouldCauseInfiniteRecursion1 = jest . fn ( ( ) => personDecoder ( alice ) ) ; expect ( wouldCauseInfiniteRecursion1 ) . not . toHaveBeenCalled ( ) ; } ) ;