|
huhu posted:Each time a new image is passed to the Image component, I want its color value to be included in the theme which OtherComponent and BarView could import from. Would there be a best practice for achieving something like this? If theme.js is just a non-component container for an object you are mutating, I’m not sure the sub components would re-render when you update it. Is there a common parent to all the components that use the theme? Maybe you can put the theme as a prop on that? smackfu fucked around with this message at 15:28 on Sep 3, 2018 |
# ? Sep 3, 2018 15:24 |
|
|
# ? May 15, 2024 04:31 |
|
huhu posted:I've got a React project Yes. There are a few ways that would be best depending on the size of your app and the bits of it we can't see from m your post. Way 1: Have the component that is a parent to both accept the change in its state, and pass the data that is needed down as props to the components that need it. Way 2: If the above means passing stuff down through many, many, many components and it is making you unhappy, use the new React Context API to get the data down where it needs to be. Way 3: if you have lots and lots and lots off application state and it needs to be sent all over to lots and lots of components that aren't easy parent child relationships, then look into a state management library like Redux or Mobx. If your app is small, you likely do not want this option.
|
# ? Sep 4, 2018 02:56 |
FSMC posted:I have quite a few functions that go through the DOM, extracts bits and changes bits, so I want to test the key functionality of my code. It will be running over a variety of x/html files, some that use various standards, ignore standards and do things in completely different ways. I think my issue is there is no way I'll ever get anywhere near being able to write good mock functions to emulate these functions acting on these html files. Why can't I test the actual functions acting on a html file? I may be missing something, but in general, I would 1) separate out any direct interaction with the dom to a separate service/library, and test that using something like jsdom 2) use pure functions as much as possible so it's easy to unit test. 3) use a linter; you have a missing var in the elementCases creation and nodes creation lines. This can bite you in the rear end because without 'var' you're creating a global. In general enable strict mode. 3) Try to have your code only do one thing. I find your function relatively difficult to understand because there's a lot of stuff interweaving. The presence of the IXBRL_ELEMENTS constant seems redundant since it's only really used as an argument to getCases(), and you could preserve the required tag on the getCases output if it returns an array of objects instead of just strings. Is it the case that nodes[k].name is equal to elementCases[j]? If so you could greatly simplify things. In general I would advise looking into lodash for some very handy tools to manipulate/transform data and objects.
|
|
# ? Sep 4, 2018 09:19 |
|
I want to delay the return statement in a map reduce by X time to gradually load in whats being returned. I figured I'd use lodash to do it but my attempts are coming up short, any ideas?
|
# ? Sep 4, 2018 13:48 |
|
Ape Fist posted:I want to delay the return statement in a map reduce by X time to gradually load in whats being returned. I figured I'd use lodash to do it but my attempts are coming up short, any ideas? Add a sleep function? (from Google) code:
|
# ? Sep 4, 2018 13:59 |
|
Map to promises with delays (using setTimeout to delay) and then Promise.all to get all of the results. Something like (phone posting):code:
|
# ? Sep 4, 2018 14:04 |
|
huhu posted:Add a sleep function? Sleeping like that isn't a good approach almost always in JS. You're blocking anything else from running during the sleep period.
|
# ? Sep 4, 2018 14:08 |
|
huhu posted:Add a sleep function? A slightly less awful sleep function: JavaScript code:
|
# ? Sep 4, 2018 14:16 |
|
huhu posted:Add a sleep function? just to harp on this, javascript is single threaded (excepting web workers) so this will block the UI thread and any timers you have running with settimeout etc. You usually don't want to sleep like this. the settimeout equivalent does not block the main thread and just executes a function after x amount of time - however, x is just earliest the function can fire and it's not guaranteed that it will fire at x time, and settimeout is also subject to the timer resolution of your operating system (e.g. the timer might be accurate to ~15ms instead of just 1) also, Date().getTime() is a bad way of getting current milliseconds because at very best, it will be accurate to 1ms. performance.now() can be accurate to the microsecond, except it's not actually because browser vendors realized that returning the real time using performance.now() made machines vulnerable to fingerprinting, so the brilliant solution is that your different browser vendors will add random garbage to the timestamp returned by performance.now() to make machines less vulnerable to fingerprinting (https://developer.mozilla.org/en-US/docs/Web/API/Performance/now - to the point where I think performance.now() is less accurate than Date().getTime() on Firefox. in conclusion the internet was a mistake.
|
# ? Sep 4, 2018 18:19 |
|
Bruegels Fuckbooks posted:
The one thing all web developers can agree on.
|
# ? Sep 5, 2018 02:00 |
|
huhu posted:Add a sleep function? youre hired
|
# ? Sep 5, 2018 02:12 |
Busy waiting (which is what that is,) and sleeping aren't the same thing, and pure busy waiting is bad concurrency practice regardless of language.
|
|
# ? Sep 5, 2018 02:33 |
|
Fish Ladder Theory posted:youre hired Joke's on you, I copied it from Stack Overflow.
|
# ? Sep 5, 2018 02:51 |
|
Joda posted:Busy waiting (which is what that is,) and sleeping aren't the same thing, and pure busy waiting is bad concurrency practice regardless of language. Edit: if you're going to busy-wait, at least mine a bitcoin or something with those 'idle' cycles.
|
# ? Sep 5, 2018 03:16 |
|
huhu posted:Joke's on you, I copied it from Stack Overflow. You're double hired.
|
# ? Sep 5, 2018 03:44 |
|
Lumpy posted:You're double hired. I will match this man's offer and triple it. Also offer you team lead position.
|
# ? Sep 5, 2018 10:47 |
|
Ape Fist posted:I will match this man's offer and triple it. This is why those fat cats at Ape Fist inc always get the best talent
|
# ? Sep 5, 2018 12:52 |
|
Obfuscation posted:But it costs money! I really like PyCharm though, so I'll check it out. I realize this is old, but PyCharm is a superset of webstorm...at lease on the professional version, not sure about community edition.
|
# ? Sep 5, 2018 17:36 |
|
Thermopyle posted:I realize this is old, but PyCharm is a superset of webstorm...at lease on the professional version, not sure about community edition. Community version of PyCharm doesn't do javascript, sadly.
|
# ? Sep 5, 2018 18:16 |
|
We've just started using Styled-Components and want to test them within Enzyme. So we have a function... code:
code:
code:
code:
|
# ? Sep 7, 2018 15:20 |
|
Osmosisch posted:I may be missing something, but in general, I would Lets use a really simple example. Say I want to test the following function on say google.com to see if it returns "google". I'm using vscode, I've used npm to install jsdom and jasmine and written a function. Now what? code:
Osmosisch posted:2) use pure functions as much as possible so it's easy to unit test. Osmosisch posted:
code:
Osmosisch posted:Is it the case that nodes[k].name is equal to elementCases[j]? If so you could greatly simplify things.
|
# ? Sep 7, 2018 21:14 |
|
Whats the difference between function = ( argument ) => { result } && function(argument) { result } ?
|
# ? Sep 8, 2018 10:26 |
|
A couple of things. Most importantly, arrow functions are not passed this as an "invisible" named argument. Instead it is captured by reference from the surrounding scope.JavaScript code:
|
# ? Sep 8, 2018 10:43 |
|
So for method2 you'd just reference id?
|
# ? Sep 8, 2018 10:49 |
|
I think you're suggesting something like:JavaScript code:
Generally you would simply not use an arrow function as a method on an object. I only did it here to demonstrate the point. In most cases it's okay to replace a regular function with an arrow function. You only have to think twice if this is involved. Or a few other things, described here with good examples.
|
# ? Sep 8, 2018 11:06 |
|
Generally, you want to use arrow functions when you're using them as functions to pass around, and function() functions when you're defining members or scoped standalone real functions. In the majority of cases doing this will give you the intuitive behavior rather than the confusing behavior. Doom Mathematic gave the example where arrow functions in the wrong place gives you confusing behavior. The example where function() functions in the wrong place gives you confusing behavior is something like: code:
|
# ? Sep 8, 2018 18:26 |
FSMC posted:This is what I'm struggling to do. I've spent at least an hour a day for the last week trying to get somewhere but I'm just going round in circles. Either I find a comment that says don't do any DOM testing or it's easy just use jest, jasmine, jsdom, jquery, etc. I haven't found a single example I can use. I've tried following various examples but then they will be missing the main bits I need help with, e.g. it'll say "now you just write the tests like normal". In principle I would say you want to do as little direct interaction with the DOM as possible in your logic, and abstract that away. One of the shallowest versions is something like: code:
Taking a step back, I'm also kind of curious why your data model is coming from HTML in the first place. There's probably easier-to-deal-with formats. quote:I'm a bit confused here, can you explain this in a way a noob like me would understand? I don't know what "required tag" means, or why I would return an array of object instead of strings. code:
code:
|
|
# ? Sep 10, 2018 22:31 |
|
E: loving christ. Figured it out. Turns out when you change the ports your flask-restful server listens to, you might want to actually change the ports in your ReactJS code as well.
Shaman Tank Spec fucked around with this message at 09:59 on Sep 12, 2018 |
# ? Sep 12, 2018 09:54 |
|
e: or that, whatever, glhf
|
# ? Sep 12, 2018 10:02 |
|
Der Shovel posted:E: loving christ. Figured it out. Turns out when you change the ports your flask-restful server listens to, you might want to actually change the ports in your ReactJS code as well. UI/API is a pretty good split. I'd suggest two things, first learn about the network tab in Chrome developer tools. Second, get something like Postman. It can make requests so you can test the backend and you can also start up a mock backend to test your frontend is behaving correctly when you give it data you expect from your mock backend.
|
# ? Sep 12, 2018 14:11 |
|
Osmosisch posted:In principle I would say you want to do as little direct interaction with the DOM as possible in your logic, and abstract that away. One of the shallowest versions is something like: Thanks. I managed to get jsdom working both with with a mock and then also got it working with a site by adding some code from https://github.com/dnajs/load-web-page-jsdom-jasmine. I had some trouble with jsdom at first sine it doesn't seem to support any of the dom manipulation functions I use. There are two use cases why want good html coverage and why data is in the html in the first place. The first is sets of accounts in ixbrl format stored as xhtml, so I need to test anything built over a variety of accounts created using various software. The second is I'm working on an extension that does it's magic by getting and adding data to different sites I have no control over, if I fix something on one site I want tests to make sure I haven't broken it on another site, etc. JSDOM seems a bit limited so I'm trying to get Karma to work. One area I'm still a bit lost with is how to properly export functions for the spec function. I've been adding "exports.function=function;" to export each function and then using require to import them into the test file. It's really clunky. With the jasmine standalone I didn't have to do that and I just run the specrunner.html which had the location of the files and it worked fine. Is there a way to set up the command-line tests to work like that? I thought that jasmine.config would just have an option where you add your src files, but I can't find anything.
|
# ? Sep 13, 2018 23:43 |
|
Is there a typescript+webpack solution for debug logging that gets completely elided from final distributed code? I want to log to the console while I'm developing, because I have a fairly complicated websocket thing going on that I'd like to easily see what communication is happening, but in a release version I'd like that logging code not just uncalled but removed. I found libraries but they don't seem to do elision. I found you can get uglify to elide useless code, define DEBUG, and then do DEBUG && console.log(...) every time, but that seems verbose. I found grunt-strip, that does what I want with grunt and js, but I don't know how I'd apply that in webpack+ts. I found webpack-strip-debug, which again seems to be applying to js not ts, maybe it could be chained after the ts compile step? Not even edit, answered my own question, but sharing because someone else might be interested: code:
code:
code:
(I could more easily just strip all console.log, by doing WebpackStripLoader.loader('console.log') instead of 'debug' but sometimes I want to leave a console.log in.)
|
# ? Sep 14, 2018 07:22 |
FSMC posted:Thanks. I managed to get jsdom working both with with a mock and then also got it working with a site by adding some code from https://github.com/dnajs/load-web-page-jsdom-jasmine. I had some trouble with jsdom at first sine it doesn't seem to support any of the dom manipulation functions I use. Mostly people run jasmine tests using Karma, which I see below you're moving towards. Karma is not a jsdom replacement, but a test runner, so you still need to load jsdom if you want to use it as your browser: https://www.npmjs.com/package/karma-jsdom-launcher . Karma does manage the part you're asking about regarding aggregating the test files to load, plus can do a whole bunch of other stuff like coverage reports. If the DOM manipulation you use is not supported by jsdom you're probably best off using a browser, probably headless chrome since Phantomjs is no longer being worked on. quote:There are two use cases why want good html coverage and why data is in the html in the first place. The first is sets of accounts in ixbrl format stored as xhtml, so I need to test anything built over a variety of accounts created using various software. The second is I'm working on an extension that does it's magic by getting and adding data to different sites I have no control over, if I fix something on one site I want tests to make sure I haven't broken it on another site, etc. Good luck.
|
|
# ? Sep 14, 2018 08:25 |
|
FSMC posted:There are two use cases why want good html coverage and why data is in the html in the first place. The first is sets of accounts in ixbrl format stored as xhtml, so I need to test anything built over a variety of accounts created using various software. The second is I'm working on an extension that does it's magic by getting and adding data to different sites I have no control over, if I fix something on one site I want tests to make sure I haven't broken it on another site, etc. in general the point of unit tests should not be to verify the end to end functionality, the point of unit tests is verify that your code in isolation. the test you're talking about is more an end to end test or an integration test. the problems with the end to end in this perspective are a) End to end testing is slower than unit testing. b) End to end testing is less reliable, as you're at the whims of the external site. c) In your case, since you're testing stuff that's not under your control, the tests might break for reasons that aren't your fault or problem. while you can test live dom manipulation using jasmine/karma, you're probably better off not using unit test tools for this kind of test, and using browser automation tools like selenium.
|
# ? Sep 14, 2018 12:29 |
|
I'm working on some problems at exercism.io, and I'm running into an issue with the tests. My code is this:code:
code:
The result is an error that seems to point to something in the syntax or the test setup: code:
|
# ? Sep 15, 2018 15:10 |
|
Hypnobeard posted:I'm just starting out in Javascript/ES6, so this is confusing the heck out of me. What am I missing, either in my file or in the test spec? You either want code:
code:
Or a different syntax of export.
|
# ? Sep 15, 2018 15:26 |
|
roomforthetuna posted:You've exported isLeap as default, but you've imported it by name. Ahh.. this cleared it up. Need to do code:
Thank you a ton!
|
# ? Sep 15, 2018 15:33 |
|
Hypnobeard posted:Removing the braces fixed it. (ESLint was complaining if I tried to shift to export const.. without a default.) So many ways of doing modules, each just different enough to be confusing. I'm using typescript now so everything is slightly different another different way.
|
# ? Sep 15, 2018 15:48 |
|
modiule.exports = {} is the old method. If you have export support you just call export with a const or function or whatever multiple times. Or as a map of things to export:code:
|
# ? Sep 15, 2018 17:08 |
|
|
# ? May 15, 2024 04:31 |
|
The export syntax is a mini-horror in itself. It came to be because they want you to only export one thing. It's why when you import you just "import thing from module" if it's the default export. Except, you need to explicitly say it's the default instead of just saying export. And if you export as default it bans you from using the other import syntax for that export. It's baffling until you read about it and their intent. Even then, it's inexplicable why anyone thought not being able to import a default through curly braces was a good idea. Or why they thought only having a single export wouldn't make it automatically the default and then require curly braces, with no default, for modules with more than one export. Or, just always or never require curly braces on imports. Instead, we have what is there now which is unintuitive and awkward. There are some shops that ban default so you can use the same import syntax in all cases. Some other places have "only use default unless you have a real good reason" and then do some high level collection of modules to export in one header-esque file type thing. Other places just mix and match as they see fit which is reasonable enough but leads to awkward situations where something is or isn't a default. Generally, you can mix and match as you see fit. Once you know how it works it's not awful to work with. code:
Khorne fucked around with this message at 20:00 on Sep 15, 2018 |
# ? Sep 15, 2018 19:47 |