Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Params not created from refPath #10

Open
ahaverty opened this issue Apr 8, 2018 · 8 comments
Open

Params not created from refPath #10

ahaverty opened this issue Apr 8, 2018 · 8 comments
Assignees

Comments

@ahaverty
Copy link

ahaverty commented Apr 8, 2018

Version info

firebase-functions-test: v0.1.1
firebase-functions: v1.0.1
firebase-admin: v5.12.0

Steps to reproduce

let data = test.database.makeDataSnapshot({
        valProp1: "hello",
        valProp2: 12345
}, "my/path/param1/param2");

await test.wrap(myFunctions.func1)(data);

Expected behavior

export let func1 = functions.database.ref('my/path/{param1}/{param2}').onCreate(async (snapshot, context) => {
    let param1: string = context.params.param1; //"param1"
    let param2: string = context.params.param2; //"param2"
    let val1: string = snapshot.val().val1; //"hello"
    let val2: number = snapshot.val().val2; //12345
)};

Actual behavior

export let func1 = functions.database.ref('my/path/{param1}/{param2}').onCreate(async (snapshot, context) => {
    let param1: string = context.params.param1; //undefined
    let param2: string = context.params.param2; //undefined
    let val1: string = snapshot.val().val1; //"hello"
    let val2: number = snapshot.val().val2; //12345
)};

Workaround

let data = test.database.makeDataSnapshot({
        valProp1: "hello",
        valProp2: 12345
}, "my/path/param1/param2");

await test.wrap(myFunctions.func1)(data,
        {
            params: {
                param1: userId,
                param2: orderId
            }
        });

It took a few debugging attempts to discover that the pathRef wasn't auto parsing into the context's params without the options in wrap().
Re-reading the docs, it looks like this is by design and similar to what firebase functions:shell was doing
However, If it's possible to automatically parse the refPath into context, please take this as a feature request 🙇.
Perhaps the docs could highlight that parsing params into context isn't a thing? 🤔

@lukepighetti
Copy link

I just ran into this and its a total headscratcher. Makes my assertions a lot more complex and brittle and it seems like it would be default behavior for context to work with the path we provide.

@lukepighetti
Copy link

lukepighetti commented Sep 25, 2018

Found a way to make it feel normal for now, although not ideal.

  const snap = fft.database.makeDataSnapshot(
    chatMessage,
    "/chats/{chat_id}/{message_id}"
  );

  const params = {
    chat_id: "existing_chat",
    message_id: "new_message"
  }

  test("that the correct association is made", async () => {
    await wrapped(snap, { params });

///blahblah

@jeremylorino
Copy link

Please fix :(

@abeisgoat
Copy link
Contributor

abeisgoat commented Nov 27, 2018

Hey folks,

I agree this is definitely confusing and it's high on my list of fixes.

In the meantime, you can automatically construct your path from a string of wildcards like this...

const pathTemplate = "/a/{wild}"
const params = {
  wild: "value"
};
const pathSnapshot = Object.keys(params).reduce((s, k) => {
    return s.replace(`{${k}}`, options.params[k]);
}, pathTemplate);
const snap = fft.database.makeDataSnapshot(val, pathSnapshot);

@abeisgoat abeisgoat self-assigned this Nov 27, 2018
@jeremylorino
Copy link

Sweet, thanks!

Keep up the good work!

@alekslario
Copy link

alekslario commented Mar 26, 2019

const pathSnapshot = Object.keys(params).reduce((s, k) => { return s.replace({${k}}, options.params[k]); }, pathTemplate);

Where do options come from in options.params?

Also is it a realtime db only solution? None of it works for me when trying to test functions+Firestore.

@tzvc
Copy link

tzvc commented Jun 17, 2020

Any updates on this ? I still run into the issue today. Happy to help if needed :)

@akarabach
Copy link

Any updates on this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants