Code Monkey home page Code Monkey logo

Comments (5)

oskarrough avatar oskarrough commented on June 15, 2024 1

That's a good plan. Thank you again! I'll leave this issue open until we have at least a basic test that verifies that it can play a YouTube video.

from ember-youtube.

lancedikson avatar lancedikson commented on June 15, 2024

The problem

I've run into a problem with this piece of code. I was testing my own component that uses ember-youtube inside of it and the test below is about changing the currentVideoId for the player.

test('it should change video by click on the next video button', function (assert) {
  assert.expect(1);

  this.render(hbs`
    {{#v-video-player 
       isOpen=true
       video=videos
       currentVideoId=currentVideoId}}
    {{/v-video-player}}
  `);

  this.$('.js-v-video-player__next').click();
  assert.equal(this.get('currentVideoId'), this.get('videos')[1].videoId);
});

.js-v-video-player__next here is a button that switches currentVideoId to the next video in the list of videos.

I've tried even this option:

return wait().then(() => {
  this.$('.js-v-video-player__next').click();
  assert.equal(this.get('currentVideoId'), this.get('videos')[1].videoId);
});

but it works not all the time. Sometimes it fails and I haven't realized what it depends on.
So, logically, firstly it starts initializing the player, but it doesn't finish before ytid changing and trying to run player initializing one more time.

The Solution

What about to save a promise from loadAndCreatePlayer to a property and return it every time when loadAndCreatePlayer is called, but the promise hasn't finished. I'd be looking like this:

     loadAndCreatePlayer() {
		let isRunning = this.get('loadAndCreatePlayerIsRunning');
		if (isRunning) {
			// some ember-concurrency would be nice here
			return this.get('loadAndCreatePlayerIsRunning');
		}
		const promise = new RSVP.Promise((resolve, reject) => {
			this.loadYouTubeApi().then(() => {
				this.createPlayer().then(player => {
					this.setProperties({
						player,
						playerState: 'ready'
					});
					this.sendAction('playerCreated', player);
					this.set('loadAndCreatePlayerIsRunning', false);
					resolve();
				})
				.catch(err => {
					if (this.get('showDebug')) {
						Ember.debug(err);
					}
					reject(err);
				});
			});
		});
    this.set('loadAndCreatePlayerIsRunning', promise);
    // The `wait` helper waits for this run loop,
		// but not the above promise, which is what i want.
		if (Ember.testing) {
			run.later(() => {}, 5000);
		}
		return promise;
	},

In that way, tests pass every time. I'm gonna test this thing a bit more and send a PR with this editings if it's needed.

@oskarrough, what do you think?

from ember-youtube.

oskarrough avatar oskarrough commented on June 15, 2024

@lancedikson very happy that you're looking into it. I think that changes makes sense. Especially when it makes it testable.

I actually started refactoring it with ember-concurrency here https://github.com/oskarrough/ember-youtube/blob/develop/addon/components/ember-youtube.js#L28 to simplify exactly this logic but didn't get to finish it up. Also ended up rewriting much more than I should have in that branch so will have to try again.

from ember-youtube.

lancedikson avatar lancedikson commented on June 15, 2024

@oskarrough great! I guess we still could have my proposed fix as a hotfix and then you can release ember-concurrency implementation whenever it's done, right? I will send a PR today if you agree :)

from ember-youtube.

oskarrough avatar oskarrough commented on June 15, 2024

@lancedikson I think this is now finally fixed… with ember 3.0 there's a test helper waitFor that can wait for an element to appear. Very handy.

from ember-youtube.

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.