Git Product home page Git Product logo

Comments (7)

willryan avatar willryan commented on September 22, 2024 1

@mattvperry should be fixed in 0.3.4. Thanks for providing a breaking test. 😄

from factory.ts.

willryan avatar willryan commented on September 22, 2024

I think that's possible, but it may require some markup on the factory. E.g.

const groceryStoreFactory = factory.makeFactory<IGroceryStore>({
    aisle: factory.nested({
       name: 'Junk Food Aisle',
       typeOfFood: 'Junk Food',
       budget: 3000
    })
})

Also, if you have an optional property in a type, but a factory declares a default real value (e.g. 123 for type number?), you will probably have to explicitly set it to undefined when you call build() if you want to switch to not having a value.

Anyway, I'm super busy for the next month, but I will put this on my list for early 2018. I might have enough time to look at a pull request, though, if someone wants to jump in. ;)

from factory.ts.

willryan avatar willryan commented on September 22, 2024

I've taken a swing at this with Nested Partials. I'm mildly afraid I've broken other stuff, particularly on the compile time side of things (which is a bit harder to write a test for). But please pull it down and see if it works for you. I'm out of time to work on this today, but as soon as I can I'll do a bit more checking and then merge it in.

from factory.ts.

mattvperry avatar mattvperry commented on September 22, 2024

This has broken a different feature:

interface Entity {
  a: string,
  b?: string
}
const factory = Factory.makeFactory<Entity>({
  a: Factory.each(i => `Entity_${i}`)
});

var e = factory.build({ b: 'test' })

console.log(e.b) // this should be 'test' but is undefined because the new code is only overwriting keys that it can find and not adding new keys.

from factory.ts.

willryan avatar willryan commented on September 22, 2024

@mattvperry Thanks for the heads up. I will try to have a fix out in the next few days.

from factory.ts.

mattvperry avatar mattvperry commented on September 22, 2024

@willryan thank you for the quick turn around!

from factory.ts.

G-Rath avatar G-Rath commented on September 22, 2024

@willryan what ever happened to this?

I've found this is pretty much a requirement for testing in TypeScript, and can't find any factory libraries for doing this.

Here's an example of the kind of types I'm working with (for a project using AwsLex):

interface LexIntent {
  name: string;
  slots: StringMap;
  slotDetails: Record<keyof LexIntent['slots'], SlotDetails>;
  confirmationStatus: 'Confirmed' | 'Denied' | 'None';
  sourceLexNLUIntentInterpretation: unknown;
}

export interface LexInputEvent {
  messageVersion: string;
  invocationSource: string;
  userId: string;
  sessionAttributes: object;
  requestAttributes: Nullable<Record<LexHeader, string>>;
  bot: LexBot;
  outputDialogMode: 'Text' | 'Voice';
  currentIntent: LexIntent;
  inputTranscript: string;
}

In Lex, LexInputEvent#currentIntent#name is a key property to figure out what logic to actually do - naturally in my unit tests usually only few of the properties (root-level or nested) are being tested at a time, so I'm wanting to be able to do something like:

Factory.makeLexInputEvent({ currentIntent: { name: 'greeting' } });

without TypeScript having a fit at me 😂

Understandably I don't expect this to be a 100% painless process, since this is in a manner going against the whole point of TypeScript, but I think it would be great to have a library that'd make this as easy as possible!

It seems that this issue fits with what I'm after, but as I said, I can't see any evidence on this issue that the feature was actually implemented, as the README doesn't mention anything close to nested types.


On usage, I see that deep nesting works like a charm:

const LexInputEventFactory = Factory.makeFactory<LexInputEvent>({
  messageVersion: '1.0',
  invocationSource: 'DialogCodeHook',
  userId: 'lex-user',
  sessionAttributes: {},
  requestAttributes: null,
  bot: {
    name: 'LexBot',
    alias: 'Bot',
    version: '$LATEST'
  },
  outputDialogMode: 'Text',
  currentIntent: {
    name: 'goodbye',
    slots: {},
    slotDetails: {},
    confirmationStatus: 'None',
    sourceLexNLUIntentInterpretation: null
  },
  inputTranscript: 'Goodbye'
});

console.log(LexInputEventFactory.build({ currentIntent: { name: 'hello world' } }));

// output:
/*
    { messageVersion: '1.0',
      invocationSource: 'DialogCodeHook',
      userId: 'lex-user',
      sessionAttributes: {},
      requestAttributes: null,
      bot: { name: 'LexBot', alias: 'Bot', version: '$LATEST' },
      outputDialogMode: 'Text',
      currentIntent:
       { name: 'hello world',
         slots: {},
         slotDetails: {},
         confirmationStatus: 'None',
         sourceLexNLUIntentInterpretation: null },
      inputTranscript: 'Goodbye' }
*/

I think you should add an example of this to the README, as like I said I think it's one of the most powerful & important features.

However, what's not supported is the ability to provide a factory in a factory:

const LexIntentFactory = Factory.makeFactory<LexIntent>({
  name: 'goodbye',
  slots: {},
  slotDetails: {},
  confirmationStatus: 'None',
  sourceLexNLUIntentInterpretation: null
});

const LexInputEventFactory = Factory.makeFactory<LexInputEvent>({
  messageVersion: '1.0',
  invocationSource: 'DialogCodeHook',
  userId: 'lex-user',
  sessionAttributes: {},
  requestAttributes: null,
  bot: {
    name: 'LexBot',
    alias: 'Bot',
    version: '$LATEST'
  },
  outputDialogMode: 'Text',
  currentIntent: LexIntentFactory,
  inputTranscript: 'Goodbye'
});

Let me know if you'd like this put into a separate issue!

from factory.ts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.