Wales survived a major fright to subdue flamboyant Fiji 29-17 at Oita Stadium and book their place in the World Cup quarter-finals.